嵌套循环不会重复迭代

时间:2018-03-24 17:10:36

标签: python pandas for-loop

作为一种处理我的python的方法,我正在尝试创建一个解决单词阶梯谜题的算法 - see here for more info on how these work

You can download the csv containing an english dictionary from here.

这是我用代码达到的地方:

import pandas as pd

alphabet = {0:'a', 1:'b', 2:'c', 3:'d', 4:'e', 5:'f', 6:'7', 7:'h', 8:'i', 9:'j', 10:'k', 11:'l', 12:'m', 13:'n', 14:'o', 15:'p', 16:'q', 17:'r', 18:'s', 19:'t', 20:'u', 21:'v', 22:'w', 23:'x', 24:'y', 25:'z'}

english = pd.read_csv('english.csv')
temp = [0, 1, 4, 19]
temp_alpha = ['a', 'b', 'e', 't']
target = tuple([2, 0, 18, 4])
original = tuple(temp)
solution = []

#loop to find solution starts here:

#builds in dictionary and false matches

if len(original) != len(target):
    print('Error: arguments must be same length')
elif len(original) == len(target):
    print('Here is your solution')
    while original != target:
        for i, v in enumerate(original):
            temp[i] = target[i]
            for j, k in enumerate(temp):
                temp_alpha[j] = alphabet[k]
                temp_alpha = ''.join(temp_alpha)
                for item in english.itertuples():
                    print(item) #to help diagnose errors
                    while temp_alpha < item[1]:
                        if temp_alpha in item[1]:
                            solution = temp_alpha
                        else:
                            temp[i] = original[i]

代码在第一个字母上表现很好,但是,在迭代通过包含英语字典的pd数据帧之后,它会在结尾处中断而不是回到第二个字母。

为什么会这样?

0 个答案:

没有答案