如何获得一个for循环来对代码while循环

问题描述:

问题部分的每次迭代重新评估:如何获得一个for循环来对代码while循环

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循环的下一次迭代。从那里一切都按原样进行,直到它在item.items()中键入For循环“for key”的值,并且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 
+3

我非常确定每次While循环循环时都会重新评估For表达式。你如何使用柜台?如果你可以包含足够的代码来复制行为,那将是最简单的。 – HugoRune 2012-07-05 21:28:27

+0

我也认为这个问题在dictionary.items() – 2012-07-05 21:33:33

+0

你并不真正显示问题在这里。你是否期待'dictionary.items()'每次都有不同的值?如果是这样,你实际上需要在那里使用'counter'。 – 2012-07-05 21:39:49

每次for循环开始时,for语句中的表达式被评估一次。由于您已将for循环放入while循环中,每次在while循环周围遇到for语句时,都会评估表达式。

你的代码还有其他错误。发布一个真实的代码示例。

我的猜测是相信OP可能要循环包含在主网络词典的'dict的和下面,就是要的一般流程。

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

你的问题似乎有点含糊,我,但基于你对结构的格式和样本代码,事情大致如下意见将让你来处理它的内容:

# 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来迭代它们,如图所示。