我在弄字典,我以前从来没有遇到过问题。我写了几个循环来帮助匹配和清除 dict,除了我不断收到以下错误。
Traceback (most recent call last):
File "C:/Users/Service02/Desktop/D/TT/test.py", line 10, in <module>
if resultDict[currentImageTest] == oldDict["image" + str(j)]:
KeyError: 'image1'
不知道为什么明显存在关键错误。使困惑。任何帮助表示赞赏!
resultDict = {"image1":1, "image2":2, "image3":3, "image4":4, "image5": 5}
oldDict = {"image1":1, "image2":22, "image3":3, "image4":47, "image5": 5}
i=1
j=1
while i<6:
currentImageTest = "image" + str(i)
while j<6:
if resultDict[currentImageTest] == oldDict["image" + str(j)]:
del resultDict[currentImageTest]
else:
pass
j+=1
i+=1
print resultDict
最终结果(已解决):
i=1
while i<6:
currentImageTest = "image" + str(i)
j=1
while j<6:
if oldDict["image" + str(j)] == resultDict[currentImageTest]:
del resultDict[currentImageTest]
break
else:
pass
j+=1
i+=1
print resultDict