简体   繁体   English

它如何在Python的复制和内存系统中工作?

[英]How does it work for Python's copy and memory system?

How does it work for Python's memory reference? 它如何用于Python的内存引用?

When I tried a program on a matrix, I used nested list. 当我在矩阵上尝试程序时,我使用了嵌套列表。 Then I found the original data was mutated, even I copied it. 然后,我发现原始数据被突变了,即使我复制了它。 I tried several way. 我尝试了几种方法。 Finally I used copy.deepcopy() to solve the problem. 最后,我使用copy.deepcopy()解决了问题。 So what's the meaning of Python's copy and reference function. 那么,Python的复制和引用函数是什么意思。 How could we avoid such s-(problems). 我们如何避免这种s-(问题)。

data = [
[1,0,0,1,0,0,1,1,1],
[1,0,1,0,0,0,0,1,0],
[1,1,0,0,0,0,0,1,0],
[1,0,1,0,0,0,0,1,0],
[1,0,0,1,0,0,1,1,1],
[0,0,0,0,0,0,0,0,0],
]
# data1 = data.copy() # нет
# data1 = data[:] # нет
# data1 = list(data) # нет
# data1 = list(data) # нет
# data1 = copy.copy(data) # нет
# for all of above five, data1 will change with data, even they differ from ID

For these five, we shall see: 对于这五个,我们将看到:

data[0][0] = 0
print(id(data))
print(id(data1))
print(data)
print(data1)
# Out:
# 4450009608
# 4450115528
# [[0, 0, 0, 1, 0, 0, 1, 1, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0], [1, 1, 0, 0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
# [[0, 0, 0, 1, 0, 0, 1, 1, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0], [1, 1, 0, 0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0]]

Thus we have to use deepcopy, such a little pit. 因此,我们必须使用Deepcopy,这样的小坑。

This is explained (with more detail) in the documentation of the copy module : 在复制模块的文档中对此进行了更详细的说明

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): 浅表复制和深度复制之间的区别仅与复合对象(包含其他对象的对象,如列表或类实例)有关:

A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. 浅表副本构造一个新的复合对象,然后(在可能的范围内)将对原始对象中找到的对象的引用插入其中。 A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. 深层副本将构造一个新的复合对象,然后递归地将原始对象中发现的对象的副本插入其中。

All the attempts you made in your first example only create shallow copies. 您在第一个示例中所做的所有尝试只会创建浅表副本。

This data1 = data.copy() and this data1 = data[:] both make copies, but only one level deep . 这个data1 = data.copy()和这个data1 = data[:]都可以复制,但是只有一层深 Below the first level, only references are copied. 在第一级以下,仅引用被复制。 Which is why you are not seeing what you expect at the second level. 这就是为什么您没有在第二层看到期望的原因。

The id s of data and data1 will be different when for example using: 例如,使用以下命令时, datadata1id会有所不同:

data1 = list(data)

But the inner lists will still point to the same objects and have the same id s, that's why you have to use deepcopy to copy the whole contents of the original data to new objects. 但是内部列表仍将指向相同的对象并具有相同的id ,这就是为什么您必须使用deepcopy将原始data的全部内容复制到新对象的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM