【问题标题】:How to make a dictionary loop back to the start in python?如何使字典循环回到python中的开头?
【发布时间】:2015-05-14 11:08:17
【问题描述】:

python中的以下代码应该采用用户输入的关键字和消息,将字母值加在一起(因此aaa的消息和bb的关键字等于ccc)我可以将消息和关键字相加,以便它等于 3,3,3 但我不知道如何将其切换回字母。

import string
step = 1
keywful=""
values = dict()
values = {letter: index for index, letter in enumerate(string.ascii_lowercase, 1)}
keyw=input("Enter your keyword for en/decryption")
msg=input("Enter your message for en/decryption")
eord=input("Enter e for encrypt or d for decrypt")
mapkeyw = [values[letter] for letter in keyw]
mapmsg = [values[letter] for letter in msg]
tot = [x + y for x, y in zip(mapkeyw, mapmsg)]
print(tot)

感谢任何帮助。 请注意,我不能使用 chr 和 ord 函数,因为它需要是字母值(a=1,b=2,c=3 等)

【问题讨论】:

  • 你可以使用print ord(input_alphabet)-96,你会得到a = 1b = 2.....z = 26,但是-96只能与小写字符
  • 你能清楚地定义你的问题陈述吗?
  • 存在问题,如果您有 555 个“eee”,则响应为 (555) -> (444)(111) 或 (555) -> (333)(222) ..... (eee) -> (ddd)+(aaa) 或 (ccc)+(bbb) .... 请阅读:How to create a Minimal, Complete, and Verifiable example

标签: python string dictionary zip


【解决方案1】:
print(''.join([string.ascii_lowercase[i-1] for i in tot]))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    相关资源
    最近更新 更多