python RuntimeError: dictionary changed size during iteration

前端 未结 5 1952
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 12:32

I have obj like this

{hello: \'world\', \"foo.0.bar\": v1, \"foo.0.name\": v2, \"foo.1.bar\": v3}

It should be expand to

{         


        
相关标签:
5条回答
  • 2020-11-30 12:56

    Rewriting this part

    def expand(obj):
        for k in obj.keys():
            expandField(obj, k, v)
    

    to the following

    def expand(obj):
        keys = obj.keys()
        for k in keys:
            if k in obj:
                expandField(obj, k, v)
    

    shall make it work.

    0 讨论(0)
  • 2020-11-30 13:00

    For those experiencing

    RuntimeError: dictionary changed size during iteration
    

    also make sure you're not iterating through a defaultdict when trying to access a non-existent key! I caught myself doing that inside the for loop, which caused the defaultdict to create a default value for this key, causing the aforementioned error.

    The solution is to convert your defaultdict to dict before looping through it, i.e.

    d = defaultdict(int)
    d_new = dict(d)
    

    or make sure you're not adding/removing any keys while iterating through it.

    0 讨论(0)
  • 2020-11-30 13:05

    I had a similar issue with wanting to change the dictionary's structure (remove/add) dicts within other dicts.

    For my situation I created a deepcopy of the dict. With a deepcopy of my dict, I was able to iterate through and remove keys as needed.Deepcopy - PythonDoc

    A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

    Hope this helps!

    0 讨论(0)
  • 2020-11-30 13:15

    You might want to copy your keys in a list and iterate over your dict using the latter, eg:

    def expand(obj):
        keys = obj.keys()
        for k in keys:
            expandField(obj, k, v)
    

    I let you analyse if the resulting behavior suits your expected results.

    0 讨论(0)
  • 2020-11-30 13:20

    Like the message says: you changed the number of entries in obj inside of expandField() while in the middle of looping over this entries in expand.

    You might try instead creating a new dictionary of the form you wish, or somehow recording the changes you want to make, and then making them AFTER the loop is done.

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