How to solve dictionary changed size during iteration error?

后端 未结 8 1978
滥情空心
滥情空心 2020-12-06 01:40

I want pop out all the large values and its keys in a dictionary, and keep the smallest. Here is the part of my program

for key,value in dictionary.items():
         


        
8条回答
  •  生来不讨喜
    2020-12-06 02:02

    Record the key during the loop and then do dictionary.pop(key) when loop is done. Like this:

    for key,value in dictionary.items():
        for key1, value1 in dictionary.items(): 
                if key1!= key and value > value1:
                    storedvalue = key
        dictionary.pop(key)  
    

提交回复
热议问题