This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging


πŸ“– preface

The process of finding light is the process of climbing up

Pure [up], the sea, the evening farewell 】 【 【 sorry 】, “contract” in the mountain, the ordinary peer 】 【 】 【 compromise, self, good [meet], [pause], the expression of the club could [listen to], brave, explore 】 【 finally found that love [is] the lighthouse in the mist, guidance on the other side of the sea, is also a desirable destination. So, baby birds, when you settle all the sounds, don’t forget to sing. A guide to young birds

Today the blogger is going to talk to you about how to use deep copy Python! Do not like spray, if you have suggestions welcome to supplement, discussion!

For more information on installation and sinicization, see VsCode download, installation and sinicization and the Python series: configuring the Python3.0 development environment for windows10! Restart VsCode after installation! 台湾国


On! Depth copy

1. Shallow copy:

To understand depth copying, let’s first look at an example of a copy list:

"" @name: Sunny Chen @test: test font @msg: this is created by Sunny Chen. Ls1 =["Mr_Chen","Sunny",[1,2,3] ls1=["Mr_Chen","Sunny",[1,2,3] ls1=["Mr_Chen","Sunny",[1,2,3] ls1= ls1.copy() Ls1 [0]="xiaojia" print(ls1,ls2) [' Mr_Chen ', 'Sunny', [1, 2, 3]] [' xiaojia ', 'Sunny', [1, 2, 3]] # modified variable in the list of data types, ls2 change, Ls1 became ls2 [2] [0] = 6666 print # (ls1, ls2) output for: [' Mr_Chen ', 'Sunny' [6666, 2, 3]] [' xiaojia ', 'Sunny' [6666, 2, 3]]Copy the code

From the above example, we can see that in the shallow copy, when lS1 is copied to ls2, when the immutable data type in ls2 is changed, s1 does not change. When the variable data type in LS2 is changed, s2 is changed.

This is due to: When ls2[0] changes, the memory pointer changes. When ls2[0] changes, the memory pointer changes. When ls2[0] changes, the memory pointer changes. Instead of storing the “little-five” memory space, it points to the “Xiaowu” memory space. Therefore, LS1 is not affected. When the variable data type in ls2 is modified and the elements in the list [1,2,3] are modified, the memory addresses of the list [1,2,3] do not change, so the memory Pointers of ls1 and ls2 both point to the list [1,2,3], so ls2 has this table, and ls1 also changes. The following example shows the difference between mutable and immutable data types:

"" @name: Sunny Chen @test: test font @msg: this is created by Sunny Chen. "" # When modifying an immutable data type Print (id(name)) #2351401446384 name="Mr_Chen" print(id(name)) #2351401447024 Lis =["zhangsan","lisi","wangwu"] print(id(lis)) #2351401111048 lis[2]="wangmazi" print(id(lis)) # 2351401111048Copy the code

We can also better understand the shallow copy from below:

  • Before modifying data:

  • After data modification:

2. Deep copy

"" @name: Sunny Chen @test: test font @msg: this is created by Sunny Chen. '' import copy # ls3=["zhangsan","lis","wnagwu",[1,2,3]] #ls5=copy. Copy (li3) # Print (ls4) ls4[0]="Mr_ChenYongJia" ['zhangsan', 'lis', 'wnagwu', [1, 2, 3]] ['Mr_ChenYongJia', 'lis', 'wnagwu', [1, 2, 3]] ls4[3][0]=666 print(ls3,ls4) ['zhangsan', 'lis', 'wnagwu', [1, 2, 3]] ['Mr_ChenYongJia', 'lis', 'wnagwu', [666, 2, 3]]Copy the code

As shown in the preceding example, LS3 has no association with LS4. This is because ls4 copies not only memory Pointers, but also creates a new memory space and copies data to the new memory space. So the new memory pointer points to the new memory space.


Here we go :Python deep copy! Share finished, go to try it!


πŸŽ‰ finally

  • For more references, see here:Chen Yongjia’s blog

  • Like the little friend of the blogger can add a concern, point a like oh, continue to update hey hey!