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

时间:2019-04-24 19:12:31

标签: python string list

为学校项目设计程序,基于2个列表的索引对用户输入进行加密/解密。我遇到的问题是为列表建立索引,并将用户输入与索引进行比较,以便输出至屏幕= 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

现在,从这里开始,应该由用户输入,通过索引比较每个列表上的位置,然后使用另一个列表将文本打印到屏幕上。我只是无法弄清楚我的索引问题。 预先感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

其他答案也可以使用,尽管您可能需要考虑使用dict以获得更快的速度。 python字典是键值对的表(技术上讲是哈希表)。该表使用键查找值。当您在dict中搜索内容时,具有恒定的查找时间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

答案 1 :(得分:0)

您可以使用.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)

答案 2 :(得分:0)

A。我不确定您要使用代码的位置或确切的任务是什么,但是我想我是为您完成的。

B。不要养成别人替你做功课的习惯,你不会学

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)

例如,字符串“您将考试失败”给出['J', '6', 'F', '0', 'A', 'Y', 'Y', '3', '4', 'A', 'Y', '8', 'D', 'Z', 'Z', 'L', '4', 'U']