想建立如下映射关系:
'a' --> 'c'
'b' --> 'd'
...
'y' --> 'a'
'z' --> 'b'
在C中我们可以很轻松的用加法实现, 但是换到python, 我想到了以下方法:
from string import ascii_lowercase as al
my_dict = dict(zip(al, [chr((ord(x)-95)%26+97) for x in al]))
看起来一点都不cool, 主要是求字符的后两位字符那里处理的不好, 大伙有没有更pythonic点的方法呢?
I don’t know why you still need to use chr and ord?? What I mean is: your requirement is very simple. You already know the 26 letters, but you are wrong by 2 digits.
The requirement has been fulfilled. I think the key to your question may not be what I answered.
Python Challenge Level 1 Walkthrough
dict(zip(al, map(lambda x : chr((ord(x) - 95) % 26 + 97), al)) )
Sit back and wait for the master...
Haha, there is no inspection part
Not Pythonic at all :(