Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condense list into string : ['z','y','x'...] -> 'zyx...' ? Python (2.7.1)

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'

?

like image 854
O.rka Avatar asked Apr 20 '11 05:04

O.rka


2 Answers

the_list = ['z', 'y', 'x', 'w', 'v', 'w', 'x', 'y', 'z']
print "".join(the_list)
like image 138
eat_a_lemon Avatar answered Sep 28 '22 11:09

eat_a_lemon


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.

like image 43
Yann Vernier Avatar answered Sep 28 '22 11:09

Yann Vernier