超过结束范围时在列表中移动?
Shifting within a list when past end range?
我创建了一个简单的程序来对用户输入的字符串执行凯撒密码。
为了让移位经过列表末尾并返回到开头,我只是复制了该列表的所有列表值。
是否有更 pythonic 的方法来实现此结果,以便它会移回开头并在移动超过列表范围末尾时继续移动?
while True:
x = input("Enter the message you would like to encrypt via a Caeser shift; or type 'exit': ")
if x == 'exit': break
y = int(input("Enter the number by which you would like to have the message Caeser shifted: "))
alphabet = list('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz')
encoded = ''
for c in x:
if c.lower() in alphabet:
encoded += alphabet[alphabet.index(c)+y] if c.islower() else alphabet[alphabet.index(c.lower())+y].upper()
else:
encoded += c
print(encoded)
如果你确实想这样做,那么最好的办法是使用模块化算法来计算 alphabet
:
中的索引
while True:
x = input("Enter the message you would like to encrypt via a Caeser shift; or type 'exit': ")
if x == 'exit': break
y = int(input("Enter the number by which you would like to have the message Caeser shifted: "))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encoded = ''
for c in x:
if c.lower() in alphabet:
i = (alphabet.index(c.lower()) + y) % 26
encoded += alphabet[i] if c.islower() else alphabet[i].upper()
else:
encoded += c
print(encoded)
一些注意事项:您不需要将字母表转换为列表:字符串也是可迭代的; dictionary 可能是更好的替代数据结构。
x = "message"
y = 10 # Caeser shift key
alphabet = list('abcdefghijklmnopqrstuvwxyz')
encoder = dict(zip(alphabet, alphabet[y:]+alphabet[:y]))
encoded = "".join(encoder[c] for c in x)
这是我能写的最好的 pythonic 方式。您甚至不需要列表,因为每个字符都有一个预定义范围的 ASCII 值。随便玩玩吧。
def encrypt(text,key):
return "".join( [ chr((ord(i) - 97 + key) % 26 + 97) if (ord(i) <= 123 and ord(i) >= 97) else i for i in text] )
ord(i)
给你 ascii 值。 97 是 'a' 的值。所以 ord(i) - 97
与在列表中搜索 i 的索引相同。将键添加到它以移动。 chr
与 ord
相反,它将 ascii 值转换回字符。
方法中只有一行代码。
我创建了一个简单的程序来对用户输入的字符串执行凯撒密码。
为了让移位经过列表末尾并返回到开头,我只是复制了该列表的所有列表值。
是否有更 pythonic 的方法来实现此结果,以便它会移回开头并在移动超过列表范围末尾时继续移动?
while True:
x = input("Enter the message you would like to encrypt via a Caeser shift; or type 'exit': ")
if x == 'exit': break
y = int(input("Enter the number by which you would like to have the message Caeser shifted: "))
alphabet = list('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz')
encoded = ''
for c in x:
if c.lower() in alphabet:
encoded += alphabet[alphabet.index(c)+y] if c.islower() else alphabet[alphabet.index(c.lower())+y].upper()
else:
encoded += c
print(encoded)
如果你确实想这样做,那么最好的办法是使用模块化算法来计算 alphabet
:
while True:
x = input("Enter the message you would like to encrypt via a Caeser shift; or type 'exit': ")
if x == 'exit': break
y = int(input("Enter the number by which you would like to have the message Caeser shifted: "))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encoded = ''
for c in x:
if c.lower() in alphabet:
i = (alphabet.index(c.lower()) + y) % 26
encoded += alphabet[i] if c.islower() else alphabet[i].upper()
else:
encoded += c
print(encoded)
一些注意事项:您不需要将字母表转换为列表:字符串也是可迭代的; dictionary 可能是更好的替代数据结构。
x = "message"
y = 10 # Caeser shift key
alphabet = list('abcdefghijklmnopqrstuvwxyz')
encoder = dict(zip(alphabet, alphabet[y:]+alphabet[:y]))
encoded = "".join(encoder[c] for c in x)
这是我能写的最好的 pythonic 方式。您甚至不需要列表,因为每个字符都有一个预定义范围的 ASCII 值。随便玩玩吧。
def encrypt(text,key):
return "".join( [ chr((ord(i) - 97 + key) % 26 + 97) if (ord(i) <= 123 and ord(i) >= 97) else i for i in text] )
ord(i)
给你 ascii 值。 97 是 'a' 的值。所以 ord(i) - 97
与在列表中搜索 i 的索引相同。将键添加到它以移动。 chr
与 ord
相反,它将 ascii 值转换回字符。
方法中只有一行代码。