首页 > 解决方案 > Make python code for searching through dictionary more efficent (vigenere cipher)

问题描述

I'm writing some code to go through the vigenere cipher with different possibilities, and then making it only add the list to the possible options that result in a word when decrypted (that way I don't get thousands of print outs, I only get told with the most likely possibilities).

Here's the code so far

def vigenere(input):
    print("VIGENERE")
    key = ""
    keyList = []
    textList = []
    for word in englishDictionary:
        key = re.sub('[\W_]+', '', word)
        if key[:len(input)] not in keyList:
            while len(key) < len(input):
                key = key + key
            keyLetters = list(key.upper())
            keyNumbers = []

            for x in keyLetters:
                keyNumbers.append(alphaNum.get(x))
            vigenereOutput = ""

            for z in list(input):
                try:
                    keyNum = int(keyNumbers[0])
                except IndexError:
                    pass
                keyNum = keyNum - 1
                inbetween = int(alphaNum.get(z)) - keyNum
                del keyNumbers[0]

                if inbetween < 1:
                    inbetween = 26 + inbetween
                vigenereOutput = vigenereOutput +    numAlpha.get(str(inbetween))

            if vigenereOutput in englishDictionary:
                keyList.append(key[:len(input)])
                textList.append(vigenereOutput)
    if len(keyList) > 0:
        for keyEntry in len(keyList):
            print("Key:", keyList[keyEntry])
            print("Text:", textList[keyEntry])
    print()

The englishDictionary is a list of all the words in my dictionary text file (fairly big file, but using a small one would ruin the point of this decoder)

Right now however it takes over 20 minutes to go through the entire dictionary... how can I speed this process up?

标签: pythonperformancedictionary

解决方案


推荐阅读