克隆和深度复制之间的区别?

时间:2012-04-27 17:11:42

标签: python cloning deep-copy

我刚刚开始编程,并正在通过“如何像计算机科学家一样思考”来完成Python。在第9章练习之前我没有遇到任何问题:

def add_column(matrix):

    """
    >>> m = [[0, 0], [0, 0]]
    >>> add_column(m)
    [[0, 0, 0], [0, 0, 0]]
    >>> n = [[3, 2], [5, 1], [4, 7]]
    >>> add_column(n)
    [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
    >>> n
    [[3, 2], [5, 1], [4, 7]]
    """

代码应该使上述doctest通过。我在最后一次测试中遇到困难:让原始列表保持不受影响。我查找了解决方案,其中包括:

x = len(matrix)

matrix2 = [d[:] for d in matrix]
for z in range(x):
    matrix2[z] += [0]
return matrix2

我的问题是:第二行为什么不能:

matrix2 = matrix[:]

当此行到位时,原始列表将被编辑以包含添加元素。 “如何成为......”指南听起来像克隆创建一个新列表,可以在不影响原始列表的情况下进行编辑。如果这是真的,那么这里发生了什么?如果我使用:

matrix2 = copy.deepcopy(matrix)

一切正常,但我并不认为克隆会失败...... 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

在您的情况下,matrix包含其他列表,因此当您执行matrix[:]时,您正在克隆matrix,其中包含对其他列表的引用。那些也没有被克隆。因此,当您编辑这些内容时,它们在原始matrix列表中仍然相同。但是,如果您将项目附加到副本(matrix[:]),则不会将其附加到原始列表。

要想象这一点,您可以使用id函数为每个对象返回唯一编号:请参阅the docs

a = [[1,2], [3,4], 5]
print 'id(a)', id(a)
print '>>', [id(i) for i in a]

not_deep = a[:]
# Notice that the ids of a and not_deep are different, so it's not the same list
print 'id(not_deep)', id(not_deep)
# but the lists inside of it have the same id, because they were not cloned!
print '>>', [id(i) for i in not_deep]

# Just to prove that a and not_deep are two different lists
not_deep.append([6, 7])
print 'a items:', len(a), 'not_deep items:', len(not_deep)

import copy
deep = copy.deepcopy(a)
# Again, a different list
print 'id(deep)', id(deep)
# And this time also all the nested list (and all mutable objects too, not shown here)
# Notice the different ids
print '>>', [id(i) for i in deep]

输出:

id(a) 36169160
>> [36168904L, 35564872L, 31578344L]
id(not_deep) 35651784
>> [36168904L, 35564872L, 31578344L]
a items: 3 not_deep items: 4
id(deep) 36169864
>> [36168776L, 36209544L, 31578344L]

答案 1 :(得分:0)

假设您有嵌套列表,复制只会复制对这些嵌套列表的引用。

>>> a = [1]
>>> b = [2]
>>> c = [a, b]
>>> c
[[1], [2]]
>>> d = c[:]
>>> d
[[1], [2]]
>>> d[1].append(2)
>>> d
[[1], [2, 2]]
>>> c
[[1], [2, 2]]

copy.deepcopy()

一样
>>> d = copy.deepcopy(c)
>>> d[1].append(2)
>>> c
[[1], [2]]
>>> d
[[1], [2, 2]]

任何可变项都是如此。 copy.deepcopy()会尝试确保它们也被复制。

值得注意的是,使用d = c[:]复制列表并不是一个非常清晰的语法。一个更好的解决方案是d = list(c)list()从任何迭代中返回一个新列表,包括另一个列表)。更明显的是copy.copy()