Iva*_*ino 1 python subtraction
我试图在Python中减去字母,但我不能以正确的方式做到这一点.
我知道如何得到ord这封信.
喜欢:
a = "a"
x = ord(a) # -> this will give me 97.
Run Code Online (Sandbox Code Playgroud)
当我尝试从该字母中减去值时,得到的结果与我想要的完全不同.
如果我减去1从b我得到的97(代表a),但现在我想减去14从b,我想达到a,然后回去z,继续减法.
a = 97
b = 98
...
z = 122
Run Code Online (Sandbox Code Playgroud)
我想继续循环使用小写字母,它位于97和之间122.
例如,如果我减去14从b,我得到的84,但我想这样做,我想获得的方式n.
b - 14 = a - 13 = z - 12 (...) and so on.
Run Code Online (Sandbox Code Playgroud)
我希望你能理解我的意思.
;)
有人能帮我一下吗 ?
此致,伊万.
我会只隔离小写字母,然后使用切片来获得优势.当您从列表的开头减去时,您将得到一个负索引,它将从列表的后面开始索引.这应该会给你你期望的行为.
>>> s = 'abcdefghijklmnopqrstuvwxyz'
>>> s.find('c')
2
>>> s[s.find('c') - 6]
'w'
Run Code Online (Sandbox Code Playgroud)
请注意,为了确保+仍然有效,您需要使用%运算符,以防您在列表中前进.
>>> s.find('x')
23
>>> s[(s.find('x') + 5) % 26]
'c'
>>> s[(s.find('c') - 6) % 26]
'w'
Run Code Online (Sandbox Code Playgroud)