How to solve dictionary changed size during iteration error?

后端 未结 8 1940
滥情空心
滥情空心 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 01:45

    In Python3, Try

    for key in list(dict.keys()):
        if condition:
            matched
            del dict[key]
    

    1 more thing should be careful when looping a dict to update its key:

    Code1:

    keyPrefix = ‘keyA’
    for key, value in Dict.items():
        newkey = ‘/’.join([keyPrefix, key])
        Dict[newkey] = Dict.pop(key)
    

    Code2:

    keyPrefix = ‘keyA’
    for key, value in Dict.keys():
        newkey = ‘/’.join([keyPrefix, key])
        Dict[newkey] = Dict.pop(key)
    

    Result of code1/code2 is:

    {‘keyA/keyA/keyB’ : ”, ‘keyA/keyA/keyA’: ”}
    

    My way to resolve this unexpected result:

        Dict = {‘/’.join([keyPrefix, key]): value for key, value in Dict.items()}
    

    Link: https://blog.gainskills.top/2016/07/21/loop-a-dict-to-update-key/

    0 讨论(0)
  • 2020-12-06 01:46

    Here is one way to solve it:

    1. From the dictionary, get a list of keys, sorted by value
    2. Since the first key in this list has the smallest value, you can do what you want with it.

    Here is a sample:

    # A list of grades, not in any order
    grades = dict(John=95,Amanda=89,Jake=91,Betty=97)
    
    # students is a list of students, sorted from lowest to highest grade
    students = sorted(grades, key=lambda k: grades[k])
    
    print 'Grades from lowest to highest:'
    for student in students:
        print '{0} {1}'.format(grades[student], student)
    
    lowest_student = students[0]
    highest_student = students[-1]
    print 'Lowest grade of {0} belongs to {1}'.format(grades[lowest_student], lowest_student)
    print 'Highest grade of {0} belongs to {1}'.format(grades[highest_student], highest_student)
    

    The secret sauce here is in the sorted() function: instead of sorting by keys, we sorted by values.

    0 讨论(0)
  • 2020-12-06 01:52

    If you want to just keep the key with the smallest value, I would do it by first finding that item and then creating a new dictionary containing only it. If your dictionary was d, something like this would do that in one line:

    d = dict((min(d.items(), key=lambda item: item[1]),))
    

    This will not only avoid any issues about updating the dictionary while iterating it, it is probably faster than removing all the other elements.

    If you must do the modifications in-place for some reason, the following would work because it makes a copy of all the keys before modifying the dictionary:

    key_to_keep = min(d.items(), key=lambda item: item[1])[0]
    
    for key in list(d):
        if key != key_to_keep:
            d.pop(key)
    
    0 讨论(0)
  • 2020-12-06 01:57

    Alternative solutions

    If you're looking for the smallest value in the dictionary you can do this:

    min(dictionary.values())
    

    If you cannot use min, you can use sorted:

    sorted(dictionary.values())[0]
    

    Why do I get this error?

    On a side note, the reason you're experiencing an Runtime Error is that in the inner loop you modify the iterator your outer loop is based upon. When you pop an entry that is yet to be reached by the outer loop and the outer iterator reaches it, it tries to access a removed element, thus causing the error.
    If you try to execute your code on Python 2.7 (instead of 3.x) you'll get, in fact, a Key Error.

    What can I do to avoid the error?

    If you want to modify an iterable inside a loop based on its iterator you should use a deep copy of it.

    0 讨论(0)
  • 2020-12-06 02:00

    As I read your loop right now, you're looking to keep only the single smallest element, but without using min. So do the opposite of what your code does now, check if value1 < minValueSoFar, if so, keep key1 as minKeySoFar. Then at the end of the loop (as Zayatzz suggested), do a dictionary.pop(minKeySoFar)

    As an aside, I note that the key1!=key test is irrelevant and computationally inefficient assuming a reasonably long list.

    minValueSoFar = 9999999;   # or whatever
    for key,value in dictionary.items():
        if value < minValueSoFar:
            minValueSoFar = value
            minKeySoFar = key
    dictionary.pop(minKeySoFar)   # or whatever else you want to do with the result
    
    0 讨论(0)
  • 2020-12-06 02:00

    An alternative solution to dictionary changed size during iteration:

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

    Better use it with caution! when using this type of code, because list(dictionary.items()) calculated when the complier enters first time to loop. Therefore any change made on dictionary won't affect process inside the current loop.

    0 讨论(0)
提交回复
热议问题