克隆和深拷贝的区别?

2024-04-19 07:53:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始编程,正在为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通过。我在最后一次测试中被卡住了:让原始列表保持不受影响。我找到了解决方案,如下所示:

^{pr2}$

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

matrix2 = matrix[:]

当这一行就位时,原始列表将被编辑以包括添加元素。“How to be…”指南让人感觉克隆创建了一个新列表,可以在不影响原始列表的情况下进行编辑。如果那是真的,这是怎么回事?如果我使用:

matrix2 = copy.deepcopy(matrix)

一切都很好,但我没有想到克隆会失败。。。 任何帮助将不胜感激!在


Tags: 代码add元素编辑列表def编程计算机
2条回答

在您的例子中,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]

以及输出:

^{pr2}$

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

>>> 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()

^{pr2}$

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

同样值得注意的是,使用d = c[:]复制列表并不是一种非常清晰的语法。一个更好的解决方案是d = list(c)list()从任何iterable返回一个新列表,包括另一个列表)。显然,更清楚的是^{}。在

相关问题 更多 >