1

代码的问题部分:

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 语句。

谢谢!

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

固定的!问题在于 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
4

3 回答 3

2

for每次 for 循环开始时,对语句中的表达式求值一次。由于您已将 for 循环放入 while 循环中,因此每次在 while 循环周围遇到 for 语句时,都会计算表达式。

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

于 2012-07-05T21:42:57.300 回答
1

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

list_of_dicts = dictionary['key']
for subdict in list_of_dicts:
    print subdict.items() # or whatever
于 2012-07-05T21:40:10.150 回答
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键似乎都与作为字典列表的 value 相关联,这是dict_list上面代码中使用的变量。

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

于 2012-07-06T00:15:33.523 回答