1

好的,所以我对列表中关于浅拷贝的追加和更改操作之间的区别有疑问。

下面是输出:

***After Shallow Copy***
a = [[1, 2, 3], ['a', 'b', 'c'], [True, False]]
b = [[1, 2, 3], ['a', 'b', 'c'], [True, False]]

***After appending in list a (Here appending in list a doesn't affect list b)***
a = [[1, 2, 3], ['a', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 2, 3], ['a', 'b', 'c'], [True, False]]

***After appending in list b (Same as above appending in b doesn't affect a)***
a = [[1, 2, 3], ['a', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 2, 3], ['a', 'b', 'c'], [True, False], [2, 'b', False]]

***After changing 2 to 10 in list a (But changing an element gets reflected in both)***
a = [[1, 10, 3], ['a', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 10, 3], ['a', 'b', 'c'], [True, False], [2, 'b', False]]

***After changing a to z in list b (Same as above changed an element in b and reflected in both)***
a = [[1, 10, 3], ['z', 'b', 'c'], [True, False], [1, 'a', True]]
b = [[1, 10, 3], ['z', 'b', 'c'], [True, False], [2, 'b', False]]
4

1 回答 1

0

STRAIGHT OUTA Python 的文档:

  • 浅拷贝构造一个新的复合对象,然后(在可能的范围内)将对原始对象中的对象的引用插入其中。

  • 深拷贝构造一个新的复合对象,然后递归地将在原始对象中找到的对象的副本插入其中。

有关更多信息,请参阅此处:https ://realpython.com/copying-python-objects/

于 2018-06-05T09:15:29.720 回答