当前位置:网站首页>Pit trodden when copying list: shallow copy and deep copy
Pit trodden when copying list: shallow copy and deep copy
2022-07-26 03:07:00 【Love to eat dog repair bags】
Preface
Use today python When doing text analysis, I suddenly encountered a problem , I originally wanted to make a copy of the list , Subsequent changes are made in the new list , Finally, I found something wrong when I checked the results , The changes of the new list are also synchronized on the original list TnT After checking for a long time, I found that there was a problem with the copy operation at the beginning , At that time, it was assumed that the list was copied directly with the assignment symbol , The old and new lists point to the same memory. Of course, the element changes will be synchronized =n=
I hereby write a more detailed article python Deep and shallow copy and list copy methods , To remind myself -OvO-
Python Deep copy and shallow copy
First of all , hypothesis a It's a list ,b=a This form is definitely not replication , If it changes a, Then it will be modified at the same time b, Because they point to the same list
Python Shallow copy
Shallow copy , To reallocate a piece of memory , Create a new object , But the element inside is the reference of each sub object in the original object .
about list Use list() Or slice operators ":" Will create a shallow copy

For tuples , Use tuple() Or slice operators ":" Will not create a shallow copy , Instead it will return a reference to the same tuple :

besides ,python The corresponding functions are also provided copy.copy() function , For any data type , Usage is as follows :

The result is the same as before .
However , When using shallow copy , If the elements in the original object are immutable , It doesn't matter ; But if the elements are variable , Shallow copies often have problems , for example :

- First, initialize list1 list , Contains a list and a tuple ; Then on list1 Perform a shallow copy , give list2. Because shallow copy of Berry's element is a reference to the original object element , therefore list2 The elements in and list1 Point to the same list and tuple object .
- So let's look down ,list1.append(100) Said to list1 List of new elements 100. This operation will not be right list2 Any impact , because list2 and list1 As a whole are two different objects , No shared memory address . After operation list2 unchanged ,list1 It will change .
- Look again. ,list1[0].append(3) Said to list1 The first list in adds elements 3. because list2 yes list1 The shallow copy ,list2 The first element in and list1 The first element in , Point to the same list , therefore list2 The first list in will also have corresponding new elements 3.
- And finally list1[1] += (50, 60), Because tuples are immutable , It's right here list1 Second tuple splicing in , Then recreated a new meta group as list1 The second element in , and list2 The new element group... Is not referenced in , therefore list2 Not affected .
Through this example , You can see clearly the possible side effects of using light copies . If you want to avoid this side effect , Copy an object completely , You need to use a deep copy . The so-called deep copy , To reallocate a piece of memory , Create a new object , And the elements in the original object , In a recursive way , Copy to the new object by creating a new sub object . therefore , The new object has nothing to do with the original object .
Python Deep copy
Deep copy , To reallocate a piece of memory , Create a new object , And the elements in the original object , In a recursive way , Copy to the new object by creating a new sub object . therefore , The new object has nothing to do with the original object .
Python China and Israel copy.deepcopy() To achieve deep copy of objects . For example, the above example is written in the following form , It's a deep copy :

————————————————
Copyright notice : This paper is about CSDN Blogger 「jq_98」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/jq_98/article/details/122715013
Flag: I want to be a day watcher :)
边栏推荐
猜你喜欢

如何根据登录测试的需求设计测试用例?

Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation

AMD64 (x86_64) architecture ABI document:

Case: using kept+haproxy to build a Web Cluster

FPGA_Vivado软件初次使用流程_超详细

朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?

How to design automated test cases?

多线程编程

My friend took 25koffer as soon as he learned automation test. When will my function test end?

Digital commerce cloud DMS dealer management system solution: DMS system realizes business Omni channel and sales data collection
随机推荐
Design of golang lottery system
[translation] cloud like internal load balancer for kubernetes?
LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
Win11麦克风权限的开启方法
Cycle and branch (I)
持续交付和DevOps是一对好基友
Multithreaded programming
How about GF Securities? Is it safe to open an account online?
在混合云中管理数据库:八个关键注意事项
Parallelloopbody in opencv
Arthas download and startup
文件操作(一)——文件简介与文件的打开方式和关闭
hello world驱动(二)-初级版
How to design automated test cases?
Image recognition (VI) | activation function
canvas——绘制文本——饼图的修改
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
软件测试岗:阿里三面,幸好做足了准备,已拿offer
万维网、因特网和互联网的区别
Qt 信号在多层次对象间传递 多层嵌套类对象之间信号传递
