Python中字典的深拷贝
我想在Python中对一个dict
(字典)进行深拷贝。可惜的是,dict
没有.deepcopy()
这个方法。我该怎么做呢?
>>> 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中使用。
4 个回答
69
Python 3.x
from copy import deepcopy
# define the original dictionary
original_dict = {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5, 'e': 6}}
# make a deep copy of the original dictionary
new_dict = deepcopy(original_dict)
# modify the dictionary in a loop
for key in new_dict:
if isinstance(new_dict[key], dict) and 'e' in new_dict[key]:
del new_dict[key]['e']
# print the original and modified dictionaries
print('Original dictionary:', original_dict)
print('Modified dictionary:', new_dict)
这样会得到:
Original dictionary: {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5, 'e': 6}}
Modified dictionary: {'a': [1, 2, 3], 'b': {'c': 4, 'd': 5}}
如果没有使用 new_dict = deepcopy(original_dict)
,就无法删除字典中的'e'元素。
为什么呢?因为如果循环是 for key in original_dict
,而你试图修改原始字典,就会出现一个运行时错误(RuntimeError):
"RuntimeError: dictionary changed size during iteration"
所以,要在遍历字典的时候进行修改,必须先复制一份字典。
下面是一个示例函数,用来从字典中删除一个元素:
def remove_hostname(domain, hostname):
domain_copy = deepcopy(domain)
for domains, hosts in domain_copy.items():
for host, port in hosts.items():
if host == hostname:
del domain[domains][host]
return domain
73
dict.copy() 是一个用来复制字典的简单方法。
id 是一个内置函数,可以告诉你变量的地址。
首先,你需要明白“为什么会出现这个问题?”
In [1]: my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]}
In [2]: my_copy = my_dict.copy()
In [3]: id(my_dict)
Out[3]: 140190444167808
In [4]: id(my_copy)
Out[4]: 140190444170328
In [5]: id(my_copy['a'])
Out[5]: 140190444024104
In [6]: id(my_dict['a'])
Out[6]: 140190444024104
在两个字典中,键 'a' 对应的列表地址是指向同一个地方的。
所以当你在 my_dict 中改变这个列表的值时,my_copy 中的列表也会跟着改变。
针对问题中提到的数据结构的解决方案:
In [7]: my_copy = {key: value[:] for key, value in my_dict.items()}
In [8]: id(my_copy['a'])
Out[8]: 140190444024176
或者你可以使用上面提到的 deepcopy 方法。
858
这样怎么样:
import copy
d = { ... }
d2 = copy.deepcopy(d)
Python 2 还是 3:
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import copy
>>> my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]}
>>> my_copy = copy.deepcopy(my_dict)
>>> my_dict['a'][2] = 7
>>> my_copy['a'][2]
3
>>>