import copy
a = [1, 2, 3, 4, [‘a‘, ‘b‘]] #原始对象
b = a #赋值,传对象的引用
c = copy.copy(a) #对象拷贝,浅拷贝
d = copy.deepcopy(a) #对象拷贝,深拷贝
a.append(5) #修改对象a
print a
[1, 2, 3, 4, [‘a‘, ‘b‘], 5]
>>> print b
[1, 2, 3, 4, [‘a‘, ‘b‘], 5]
>>> print c
[1, 2, 3, 4, [‘a‘, ‘b‘]]
>>> print d
[1, 2, 3, 4, [‘a‘, ‘b‘]]
a[4].append(‘c‘) #修改对象a中的[‘a‘, ‘b‘]数组对象
>>> print a
[1, 2, 3, 4, [‘a‘, ‘b‘, ‘c‘], 5]
>>> print b
[1, 2, 3, 4, [‘a‘, ‘b‘, ‘c‘], 5]
>>> print c
[1, 2, 3, 4, [‘a‘, ‘b‘, ‘c‘]]
>>> print d
[1, 2, 3, 4, [‘a‘, ‘b‘]]
深copy是复制了之前的列表,两个列表从此以后就独立的两个列表了
浅copy只是独立了一层的列表
深copy和潜copy的问题
原文地址:http://4249964.blog.51cto.com/4239964/1559716