当前位置:网站首页>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 :)
边栏推荐
- STM32——串口学习笔记(一个字节、16位数据、字符串、数组)
- Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
- STM32 - serial port learning notes (one byte, 16 bit data, string, array)
- STM32——DMA笔记
- 微信公众号互助、开白群,小白报团取暖
- Cycle and branch (I)
- canvas——绘制曲线——挂钟,饼图,五角星
- Win11 hide input method status bar method
- FPGA_Vivado软件初次使用流程_超详细
- YOLOv3: An Incremental Improvement
猜你喜欢
随机推荐
Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
[steering wheel] tool improvement: common shortcut key set of sublime text 4
[SQL] 自连接的用法
多线程编程
Vofa+ serial port debugging assistant
LeetCode·83双周赛·6128.最好的扑克手牌·模拟
[SQL] CASE表达式
Self-supervised learning method to solve the inverse problem of Fokker-Planck Equation
[sql] case expression
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
图像识别(六)| 激活函数
中国信通院陈屹力:降本增效是企业云原生应用的最大价值
STM32——PWM学习笔记
如何根据登录测试的需求设计测试用例?
Detailed explanation of extended physics informedneural networks paper
[untitled]
STM32 - DMA notes
Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
OxyCon 2022 网络抓取前沿大会即将开启!
Anti electronic ink screen st7302



![[untitled]](/img/6f/a2cd98af7a8de469e5311422b48afe.png)



