python - How to convert "ABCD…" to "CDEF…" in a Pythonic way? -


for example, have lst = "abcxyz", , expect output "cdezab"

the best method i've come far is:

"".join(chr((ord(x)-ord('a')+2)%26+ord('a')) x in lst) 

which awkward. using dictionary {'a': 'c', 'b': 'd', ……, 'x': 'z', 'y': 'a', 'z': 'b'} seems more pythonic, more awkward.

any ideas?

you can build dictionary:

>>> import string >>> dict(zip(string.uppercase, string.uppercase[2:]+string.uppercase[:2])) {'a': 'c', 'c': 'e', 'b': 'd', 'e': 'g', 'd': 'f', 'g': 'i', 'f': 'h', 'i': 'k', 'h': 'j', 'k': 'm', 'j': 'l', 'm': 'o', 'l': 'n', 'o': 'q', 'n': 'p', 'q': 's', 'p': 'r', 's': 'u', 'r': 't', 'u': 'w', 't': 'v', 'w': 'y', 'v': 'x', 'y': 'a', 'x': 'z', 'z': 'b'} 

and use it:

>>> m = dict(zip(string.uppercase, string.uppercase[2:]+string.uppercase[:2])) >>> ''.join(map(m.get, "abcxyz")) 'cdezab' 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -