遍历ord中的多个字符列表

2024-10-03 00:21:26 发布

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

我想找一个字母字符串,长度在1-4个字符之间。你知道吗

首先,我重复了52个字母的列表:

letters = string.ascii_letters

然后,我需要在同一个列表中遍历字符串的下3个字符,直到找到我要查找的字符串。你知道吗

如果每个u代表52个字母的列表,我基本上需要这样做,同时在每次迭代中检查匹配:

_
_ _
_ _ _
_ _ _ _

如何最好地构造一系列循环来实现这一点?你知道吗


如果问题的前提看起来很混乱,那么这是一个关于暴力破解的问题集。我只提取了我正在努力解决的问题的一部分。你知道吗


编辑:到目前为止,这就是我要去的地方。你知道吗

#we know the salt is the 2-digit '50'
#we know the key is limited to 4 alphabetical letters
#cycle through all possibilities of the key till we match the hash

letters = string.ascii_letters
lcounter = 0
i = 0
j = 0
k = 0
l = 0
tryhash = "a"
word = [letters[i]]

while(tryhash != hash):
    for c in letters:
        word = [letters[i]]  #this does not work as the additional letters need to be appended to word after the first lcounter loop
        tryword = ''.join(word)
        tryhash = crypt.crypt(tryword, "50")

        if (tryhash == hash):
            print(word)
            break

        i += 1

        if (lcounter > 0) and (i == 52):
            i = 0
            if (lcounter == 1) and (j == 0):
                word.insert(lcounter, letters[j])
            j += 1

            if (lcounter > 1) and (k == 52):
                j = 0
                if (lcounter == 2) and (k == 0):
                    word.insert(lcounter, letters[k])
                k += 1

                if (lcounter > 2) and (k == 52):
                    k = 0
                    if (lcounter == 3) and (l == 0):
                        word.insert(lcounter, letters[l])
                    l += 1

    lcounter += 1

Tags: andtheto字符串列表if字母hash
2条回答

可能是这样的:

my_string = "some"
for letter1 in string.ascii_letters:
    if letter1 == my_string:
        print("success")
    for letter2 in string.ascii_letters:
        if letter1 + letter2 == my_string:
            print("success")
        for letter3 in string.ascii_letters:
            if letter1 + letter2 + letter3 == my_string:
                print("success")
            for letter4 in string.ascii_letters:
                if letter1 + letter2 + letter3 + letter4 == my_string
                    print("success")

你可以这样做:


    import string
    import itertools

    data = string.ascii_lowecase

    for i in itertools.permutations(data, 4):

        if i == 'your_string':
            #do something
        else:
            pass

相关问题 更多 >