深度复制和浅复制的区别是什么?


当前回答

为了避免混淆浅复制和简单地为list分配一个新变量名,再添加一点。

“假设我们有:

x = [
    [1,2,3],
    [4,5,6],
    ]

这个语句创建了3个列表:2个内部列表和一个外部列表。然后,外部列表的引用在名称x下可用

y = x

没有数据被复制。我们在内存的某个地方仍然有相同的3个列表。所有这一切所做的是使外部列表在名称y下可用,除了它之前的名称x

y = list(x)

or

y = x[:]

这将创建一个与x内容相同的新列表。列表x包含对两个内部列表的引用,因此新列表也将包含对这两个内部列表的引用。只复制了一个列表——外层列表。 现在内存中有4个列表,两个内部列表,一个外部列表,以及外部列表的副本。原始外部列表的名称为x,新的外部列表的名称为y。

内部列表没有被复制!此时,您可以从x或y访问和编辑内部列表!

如果你有一个二维(或更高)的列表,或者任何类型的嵌套数据结构,并且你想对所有内容进行完整复制,那么你想在复制模块中使用deepcopy()函数。你的解决方案也适用于2-D列表,迭代外部列表中的项目,并对每个项目进行复制,然后为所有内部副本构建一个新的外部列表。”

来源:https://www.reddit.com/r/learnpython/comments/1afldr/why_is_copying_a_list_so_damn_difficult_in_python/

其他回答

为了避免混淆浅复制和简单地为list分配一个新变量名,再添加一点。

“假设我们有:

x = [
    [1,2,3],
    [4,5,6],
    ]

这个语句创建了3个列表:2个内部列表和一个外部列表。然后,外部列表的引用在名称x下可用

y = x

没有数据被复制。我们在内存的某个地方仍然有相同的3个列表。所有这一切所做的是使外部列表在名称y下可用,除了它之前的名称x

y = list(x)

or

y = x[:]

这将创建一个与x内容相同的新列表。列表x包含对两个内部列表的引用,因此新列表也将包含对这两个内部列表的引用。只复制了一个列表——外层列表。 现在内存中有4个列表,两个内部列表,一个外部列表,以及外部列表的副本。原始外部列表的名称为x,新的外部列表的名称为y。

内部列表没有被复制!此时,您可以从x或y访问和编辑内部列表!

如果你有一个二维(或更高)的列表,或者任何类型的嵌套数据结构,并且你想对所有内容进行完整复制,那么你想在复制模块中使用deepcopy()函数。你的解决方案也适用于2-D列表,迭代外部列表中的项目,并对每个项目进行复制,然后为所有内部副本构建一个新的外部列表。”

来源:https://www.reddit.com/r/learnpython/comments/1afldr/why_is_copying_a_list_so_damn_difficult_in_python/

广度vs深度;从引用树的角度考虑,将对象作为根节点。

浅:

变量A和B指的是不同的内存区域,当B被分配给A时,这两个变量指的是相同的内存区域。后来对其中一个内容的修改立即反映在另一个的内容中,因为它们共享内容。

深:

变量A和B指的是不同的内存区域,当B被分配给A时,A所指向的内存区域中的值被复制到B所指向的内存区域。后来对其中一项内容的修改仍为A或B所独有;内容不共享。

“ShallowCopy”指向与“Source”相同的内存位置。“DeepCopy”指向内存中的不同位置,但内容是相同的。

The copy constructor is used to initialize the new object with the previously created object of the same class. By default compiler wrote a shallow copy. Shallow copy works fine when dynamic memory allocation is not involved because when dynamic memory allocation is involved then both objects will points towards the same memory location in a heap, Therefore to remove this problem we wrote deep copy so both objects have their own copy of attributes in a memory. In order to read the details with complete examples and explanations you could see the article C++ constructors.

我想给出一个例子,而不是正式的定义。

var originalObject = { 
    a : 1, 
    b : 2, 
    c : 3,
};

这段代码显示了一个浅拷贝:

var copyObject1 = originalObject;

console.log(copyObject1.a);         // it will print 1 
console.log(originalObject.a);       // it will also print 1 
copyObject1.a = 4; 
console.log(copyObject1.a);           //now it will print 4 
console.log(originalObject.a);       // now it will also print 4

var copyObject2 = Object.assign({}, originalObject);

console.log(copyObject2.a);        // it will print 1 
console.log(originalObject.a);      // it will also print 1 
copyObject2.a = 4; 
console.log(copyObject2.a);        // now it will print 4 
console.log(originalObject.a);      // now it will print 1

这段代码显示了一个深度拷贝:

var copyObject2 = Object.assign({}, originalObject);

console.log(copyObject2.a);        // it will print 1 
console.log(originalObject.a);      // it will also print 1 
copyObject2.a = 4; 
console.log(copyObject2.a);        // now it will print 4 
console.log(originalObject.a);      // !! now it will print 1 !!