首页 > 解决方案 > 如何将输入转换为每个字母包含一个字符的列表?

问题描述

我想制作一个可以破解密码的密码破解器。我不明白如何将代码的 (n) 字母放入字符串并将其转回列表并将其打印到终端中。

#This is python code
alphabet = "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 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".split() #needs double alphabet as code will add up: w --> h, or the list will be out of range
while True:
    saves = []
    crack = ""
    totalSaves = []
    code = input("Enter code >")
    codeIndex = 0
    for i in range(26): #Length of alphabet
        for i in range(len(code)): #length of code
            #get the first letter of code into the saves code in here
            #use saves[codeIndex] if possible
            #I want the code breaker to be sort of like this: a --> c. At this case, I want codeIndex to be 2. If possible, I want to put the letters into saves[].
        crack = "".join(saves) #unsure about this use
        totalSaves.append(crack)
        saves = []
        crack = ""
    printIndex = 0
    for i in range(26):
        print(totalSaves[printIndex])
        totalSaves += 1

如果你不明白,我很抱歉。

标签: pythonstringlist

解决方案


如果你想打破和组合列表中的字母,你可以这样做。

alphabet = "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 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"
alphabet_list = alphabet.split()
split_n = 5

result = []
for i in range(0, len(alphabet_list), split_n):
    result.append("".join(alphabet_list[i:i+split_n]))

print(result)

你会得到输出:['abcde', 'fghij', 'klmno', 'pqrst', 'uvwxy', 'zabcd', 'efghi', 'jklmn', 'opqrs', 'tuvwx', 'yz']


推荐阅读