How to copy a dictionary and only edit the copy

前端 未结 20 2387
说谎
说谎 2020-11-21 06:59

Can someone please explain this to me? This doesn\'t make any sense to me.

I copy a dictionary into another and edit the second and both are changed. Why is this hap

20条回答
  •  一整个雨季
    2020-11-21 07:56

    You can use directly:

    dict2 = eval(repr(dict1))
    

    where object dict2 is an independent copy of dict1, so you can modify dict2 without affecting dict1.

    This works for any kind of object.

提交回复
热议问题