python RuntimeError: dictionary changed size during iteration

前端 未结 5 1955
佛祖请我去吃肉
佛祖请我去吃肉 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 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!

提交回复
热议问题