python深复制浅复制_Python浅复制和深复制

python深复制浅复制

Before understanding the concept of shallow and deep copy, we must first clear (if any) doubts around how copy in python works.

在理解浅层复制和深层复制的概念之前,我们必须首先清除(如果有)对python中复制如何工作的怀疑。

In python, just like any other programming language, we can use the operator = to copy the value of one variable to another.

与其他任何编程语言一样,在python中,我们可以使用operator =将一个变量的值复制到另一个变量。

In python, when we use the = operator to assign some value to a variable currently referenced by some other variable, no new copy of the data gets created rather the same copy of data is then referenced by two different variables/objects.

在python中,当我们使用=运算符为当前由某个其他变量引用的变量分配值时,不会创建数据的新副本,而是由两个不同的变量/对象引用同一数据副本。

For example,

例如,

old_str = "Hello World!"
new_str = old_str

print("Id of old string:" + old_str +" is: ", id(old_str))
print("Id of new string:" + new_str +" is: ", id(new_str))

Id of old string:Hello World! is: 139681093231344 Id of new string:Hello World! is: 139681093231344

旧字符串的ID:Hello World! 是:139681093231344新字符串的ID:Hello World! 是:139681093231344

As you can see in the code above that the same string is getting referenced by both the variables. And it's not just in case of string but for every object, well everything in python is object.

如您在上面的代码中看到的,两个变量都引用了相同的字符串。 而且不仅是字符串的情况,而且对于每个对象,Python中的所有对象都是对象。

Well, then how can we actually implement deep copy and shallow copy in python, let's see.

好吧,那我们如何才能真正在python中实现深层复制和浅层复制。

Python copy模块 (Python copy Module)

Python has a copy module that allows a user to copy an object from one variable to another. As we know that when we assign one variable to another then both the variable just refers to a single object. So, we use the copy module of python to manage the copying process of an object.

Python有一个复制模块 ,该模块允许用户将一个对象从一个变量复制到另一个变量。 众所周知,当我们将一个变量分配给另一个变量时,两个变量都只引用一个对象。 因此,我们使用python的复制模块来管理对象的复制过程。

Two functions of the copy module are majorly used:

复制模块主要使用两个功能:

  • copy.copy()

    copy.copy()

  • copy.deepcopy()

    copy.deepcopy()

Let's understand how we can use both of these function one by one.

让我们了解如何才能同时使用这两个功能。

Python深度复制 (Python Deep Copy)

copy.deepcopy(x) function is used for performing deep copy in python. This method is used to create a deep copy of any object x. For deep copy, an extra individual object is created by taking the data from the main object and replicating it in the new object. So, if there is any change in the copied reference, the main object will remain as it is.

copy.deepcopy(x)函数用于在python中执行深度复制。 此方法用于创建任何对象x的深层副本。 对于深度复制,通过从主对象获取数据并将其复制到新对象中来创建额外的单个对象。 因此,如果复制的引用中有任何更改,则主要对象将保持原样。

For example:

例如:

import copy

l1=[1,3,[9,4],6,8]
l2=copy.deepcopy(l1) #Making a deep copy

print('List 1 = ', l1)
print('List 2 = ', l2)

print('Performing change in list 2')
l2[2][0] = 5

print('List 1 = ',l1)
print('List 2 = ',l2)

List 1 =  [1, 3, [9, 4], 6, 8] List 2 =  [1, 3, [9, 4], 6, 8] Performing change in list 2 List 1 =  [1, 3, [9, 4], 6, 8] List 2 =  [1, 3, [5, 4], 6, 8]

列表1 = [1,3,[9,4],6,8]列表2 = [1,3,[9,4],6,8]在列表2中执行更改列表1 = [1,3,[ 9,4],6,8]清单2 = [1,3,[5,4],6,8]

As we can see in the above example, l1(first list) is a deep copy of l2(second list). So, when we updated some data item in l2 then it didn't reflect in l1.

正如我们在上面的示例中看到的那样, l1 (第一列表)是l2 (第二列表)的深层副本。 因此,当我们更新l2某些数据项时,它并没有反映在l1

Python浅拷贝 (Python Shallow Copy)

copy.copy(x) function is used for performing shallow copy in python. This method is used to create a shallow copy of any object x. For the shallow copy, a reference of an object is copied to another object. So if there is any change on the copied reference, it will change the content of the main object also. In a way this works just like normal copying works in python.

copy.copy(x)函数用于在python中执行浅表复制。 此方法用于创建任何对象x的浅表副本。 对于浅表副本,将一个对象的引用复制到另一个对象。 因此,如果复制的引用上有任何更改,它也会更改主对象的内容。 在某种程度上,这就像在python中正常复制一样。

For example:

例如:

import copy

l1 = [1,3,[9,4],6,8]
l2 = copy.copy(l1) #Making a shallow copy

print('List 1 = ', l1)
print('List 2 = ', l2)

print('Performing change in list 2')
l2[2][0] = 5

print('List 1 = ',l1)
print('List 2 = ',l2)

List 1 =  [1, 3, [9, 4], 6, 8] List 2 =  [1, 3, [9, 4], 6, 8] Performing change in list 2 List 1 =  [1, 3, [5, 4], 6, 8] List 2 =  [1, 3, [5, 4], 6, 8]

列表1 = [1,3,[9,4],6,8]列表2 = [1,3,[9,4],6,8]在列表2中执行更改列表1 = [1,3,[ 5,4],6,8]清单2 = [1,3,[5,4],6,8]

As you can be seen in the above example, l2(second list) is provided with the shallow copy of l1(first list). So this means that l2 has the reference to the same object as to which l1 has. So, if we make any changes in l2, it will also change the listl1.

从上面的示例中可以看出, l2 (第二个列表)提供了l1 (第一个列表)的浅表副本。 因此,这意味着l2引用了与l1相同的对象。 因此,如果我们对l2进行任何更改,它也会更改列表l1

翻译自: https://www.studytonight.com/python/python-shallow-and-deep-copy

python深复制浅复制

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值