将用户输入与列表“A”进行比较,并根据列表“A”的索引将输入的字符转换为列表“B”

2024-10-02 16:33:21 发布

您现在位置:Python中文网/ 问答频道 /正文

一个学校项目的程序设计基于两个列表的索引对用户输入进行加密/解密。我的问题是为列表编制索引,并将用户输入与索引进行比较,以便屏幕的输出=list2

所有的代码都非常基本,在变量名中使用'str'和'input',以免混淆我自己。你知道吗

list1 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','.','1','2','3','4','5','6','7','8','9','0']

list2 = ['4','R','5','G','Z','3','2','D','A','E','X','Y','U','I','6','W','7','O','V','8','F','Q','0','L','J','.','H','9','C','B','N','S','P','M','1','T','K']
strInput = input("Type the message you would like to Encrypt  ").upper()
inputList = split(strInput)
print(inputList)
i = 0

for char in inputList:
    if inputList[i] != list1[i]:
    i = i + 1

现在,它应该接受用户输入,通过索引比较每个列表上的位置,然后使用另一个列表将文本打印到屏幕上。我就是想不出我的索引问题。 提前感谢您的帮助!你知道吗


Tags: the项目代码用户列表input屏幕type
3条回答

您可以使用.index(char)在第一个列表中找到字符的索引

list1 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','.','1','2','3','4','5','6','7','8','9','0']

list2 = ['4','R','5','G','Z','3','2','D','A','E','X','Y','U','I','6','W','7','O','V','8','F','Q','0','L','J','.','H','9','C','B','N','S','P','M','1','T','K']

str_input = 'TEST'

encrypted_chars = []

for char in str_input:
    if char == ' ':
        encrypted_chars.append(char)
    else:
        encrypted_chars.append(list2[list1.index(char)])

encrypted_message = ''.join(encrypted_chars)  # 8ZV8

您还可以使用python的列表理解docs

encrypted_chars = [(list2[list1.index(char)] if char != ' ' else ' ') for char in str_input]
encrypted_message = ''.join(encrypted_chars)

要解密你基本上会做相反的事情。你知道吗

decrypted_chars = [(list1[list2.index(char)] if char != ' ' else ' ') for char in encrypted_message]
decrypted_message = ''.join(decrypted_chars)

答:我不知道你的代码要去哪里,也不知道作业是什么,但我想我是为你做的。你知道吗

别养成让别人帮你做作业的习惯,你不会学到东西的

list1 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','.','1','2','3','4','5','6','7','8','9','0']​
list2 = ['4','R','5','G','Z','3','2','D','A','E','X','Y','U','I','6','W','7','O','V','8','F','Q','0','L','J','.','H','9','C','B','N','S','P','M','1','T','K']

inputList = input("Type the message you would like to Encrypt  ").upper().split()
print(inputList)

encrypted_message=[]
for word in inputList:
   for char in word:
       encrypted_message.append(list2[list1.index(char)])

print(encrypted_message)

例如字符串“youwill fail the exam”给出['J', '6', 'F', '0', 'A', 'Y', 'Y', '3', '4', 'A', 'Y', '8', 'D', 'Z', 'Z', 'L', '4', 'U']

其他答案也适用,不过您可能需要考虑使用dict来提高速度。python字典是一个键值对表(hash表是技术性的)。表使用键查找值。当您在dict中搜索某个元素时,is有一个恒定的查找时间O(1),这意味着dict不会通过自身搜索该元素。它确切地知道它在哪里(如果它有)。你知道吗

例如:

d = {
   2: 'A',
   5: 3
}
print(d[2]) # This will print the letter A
print(d[5]) # This will print the number 3

您的列表:

list1 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','.','1','2','3','4','5','6','7','8','9','0']

list2 = ['4','R','5','G','Z','3','2','D','A','E','X','Y','U','I','6','W','7','O','V','8','F','Q','0','L','J','.','H','9','C','B','N','S','P','M','1','T','K']

变成dict

# The following will zip your two lists together into a dictionary
# list1 will be the keys, and list2 will be the values.
encription_dict = {list1[i]: list2[i] for i in range(len(list1))}

然后我们可以加密:

# Get the user input
strInput = input("Type the message you would like to Encrypt  ").upper()

# The following is list comprehension 
new_str = [(key_val[char] if char != ' ' else ' ') for char in strInput]

# That one line was equivalent to:
# new_str = []
# for char in strInput:
#     if char != ' ': # if it is not a space
#         new_str.append(key_val[char]) # Add the encrypted char for this char
#     else:
#         new_str.append(' ')

# Turn the list into a single str.
new_str = ''.join(new_str)

print(new_str)

测试:
输入:Test 2
输出:8ZV8 C

相关问题 更多 >