If I have list='abcdedcba'
and i want: a=z, b=y, c=x, d=w, e=v so it would translate to:
translate='zyxwvwxya'
How would I do this? If I construct a dictionary
>>> d=dict(zip(('a','b','c','d','e'),('z','y','x','w','v')))
and type
>>> example= d[x] for x in list
>>> print translate
['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']
How do I get it back into the form
translate='zyxwvwxyz'
?
the_list = ['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']
print "".join(the_list)
For monoalphabetic substitution, use maketrans and translate from the string module. They operate like the unix tr command. Joining with an empty separator is the correct answer for that last step, but not necessary for this exact task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With