我想在 python 中制作 dict
的深层副本。不幸的是, .deepcopy()
dict
。我怎么做?
>>> my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]}
>>> my_copy = my_dict.deepcopy()
Traceback (most recent calll last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'deepcopy'
>>> my_copy = my_dict.copy()
>>> my_dict['a'][2] = 7
>>> my_copy['a'][2]
7
最后一行应该是 3
。
我希望 my_dict
中的修改不会影响快照 my_copy
。
我怎么做?该解决方案应与 Python 3.x 兼容。
原文由 Olivier Grégoire 发布,翻译遵循 CC BY-SA 4.0 许可协议
怎么样:
Python 2 或 3: