如何在while循环的每次迭代中重新计算for循环

时间:2012-07-05 21:20:04

标签: python for-loop while-loop evaluation flow

代码的问题部分:

while counter < length:
    if something:
    else:
        for key,value in dictionary.items():
            if something:
            else:
    counter += 1

结构是{ key:[ {key:value,key1:value1}, {key:value,key1:value1} ] } 这是计数器(列表索引)的用途。

因此,在我自学Python(3.2.3)时,我已经在一些脚本中注意到了这几次。

我尝试搜索但没有运气(也许我的搜索条件有误,谁知道?)我检查了文档,似乎问题是,对于while语句,它说:“while语句用于只要表达式为真,就重复执行“。对于For语句,它说:“表达式列表被评估一次”。

现在,当我的脚本运行时,它会不断出现密钥错误,因为计数器增加了1;但是for语句没有更新以反映列表中的新元素,这是另一个字典;它仍在使用第一个元素。

我假设while循环中的每次迭代都会导致For语句重新计算。显然我错了。

现在我可以重写代码,解决这个问题,但是我想知道几次已经遇到过这个问题,我怎么能以这种方式在while语句中使用For循环呢?让它通过while循环重新评估每次迭代的For语句。

谢谢!

更新:我在下面添加了实际代码。如果我只是做错了什么就不会让我感到惊讶。我正在观察调试器中的值,当count从0变为1时,它开始于while循环的下一次迭代。从那里开始,一切都按原样进行,直到它在for循环中找到“for key,value in item.items():”,而item.items()的值根本没有变化。 更新更新:在尝试弄清楚之后我很确定它是v中的项目:这导致了问题。让我们看看我能搞清楚!

固定!问题在于v中的For项:在while循环之外,通过将其移动到循环中来解决。

def checkDifferences(newFile,oldFile):
    resultDiff = []
    for k,v in newFile.items():
        if newFile[k] != oldFile[k]:
            for item in v:
                count = 0
                lengthList = len(newFile[k])
                newKeys = newFile[k][count].keys()
                oldKeys = oldFile[k][count].keys()                
                while count < lengthList:
                    if newKeys == oldKeys:
                        for key,value in item.items():
                            newValue = newFile[k][count][key] 
                            oldValue = oldFile[k][count][key]
                            if newValue == oldValue:
                                pass
                            else:
                                resultDiff.append((k,count,key, oldValue,newValue))
                    else:
                        newValue = list(newFile[k][count].keys())
                        oldValue = list(oldFile[k][count].keys())
                        keyList = [key for key in newValue if key not in oldValue]
                        print(keyList)
                        for key,value in item.items():
                            if key in keyList:
                                oldValue = oldFile[k][count][oldKey]
                                resultDiff.append((k,count,key, None,newValue))
                            else:
                                oldValue = oldFile[k][count][key]
                            newValue = newFile[k][count][key]
                            if newValue == oldValue:
                                pass
                            else:
                                resultDiff.append((k,count,key, oldValue,newValue))
                            oldKey = key
                    count += 1
     return resultDiff

3 个答案:

答案 0 :(得分:1)

我的猜测是相信OP可能想要遍历主词典中包含的'dict,以下是一般流程。

list_of_dicts = dictionary['key']
for subdict in list_of_dicts:
    print subdict.items() # or whatever

答案 1 :(得分:1)

每次for循环开始时,for语句中的表达式都会被计算一次。由于你已经将for循环放入while循环中,每次都在while循环中,当遇到for语句时,将计算表达式。

您的代码还有其他问题。发布真实的代码示例。

答案 2 :(得分:1)

您的问题似乎对我来说有点模糊,但根据您对结构格式和示例代码的评论,以下几行内容可以让您处理其内容:

# for structure { key:[ {key:value,key1:value1}, {key:value,key1:value1}, ... ] }

for dict_list in structure.values():
    if something:
    else:
        for key,value in dict_list.items():
            if something:
            else:

每次从外部for字典中检索另一个字典列表时,将评估内部structure循环。我使用了structure.values(),因为您似乎对structure的密钥不感兴趣。每个structure的键似乎都与值相关联,这是一个字典列表,上面的代码中使用了dict_list变量。

在Python中,经常不需要计数器或循环索引来处理容器(如列表或字典)的内容,因为您可以使用for直接迭代它们,如图所示。