繁体   English   中英

如何让python一次识别多个字母

[英]how to get python to recognize more than one letter at a time

所以我目前的任务是使用双重编码制作密码。 所以我有一个变量,它是字母 aa 到 zz 的列表。 另一个变量是打乱的同一个列表的副本。 然后我将 Alpha 对作为字典中的键插入,并将该对作为字典中的值插入。 我现在遇到的问题是让它一次查看一封以上邮件中的一封信。

我尝试只创建一个新变量和一个 for 循环来简单地运行它,但它一次只查看一个字母


import random

alpha= 'abcdefghijklmnopqrstuvwxyz '
alphalist= list(alpha)
alphapair= []

for let1 in alphalist:
    for let2 in alphalist:
        pair = let1+let2
        alphapair.append(pair)

paircopy= alphapair[:]

random.seed(6767)
random.shuffle(paircopy)

incoding_cipher=dict(zip(alphapair,paircopy))

message=input("Please type the message you would like to encode: ") #optional to allow for an input to encode
message= message.lower()
incoded_message=''

for let in message:
    incoded_message += incoding_cipher[let]

print(incoded_message)

这样的事情对你有用:

msgtemp = (message + ' ') if (len(message) % 2) else message
for i in range(0, len(msgtemp), 2):
    pair = msgtemp[i] + msgtemp[i + 1]
    incoded_message += incoding_cipher[pair]

如果消息长度为奇数,这会在消息的末尾添加一个空格。

改变:

for let in message:
    incoded_message += incoding_cipher[let]

到:

for first, second in zip(message[::2], message[1::2] + ' ' * (len(message) % 2)):
    key = first + second
    incoded_message += incoding_cipher[key]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM