当前位置:网站首页>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 :)
边栏推荐
- Vofa+ serial port debugging assistant
- Swin Transformer【Backbone】
- Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
- 对于稳定性测试必需关注的26点
- Win11大小写提示图标怎么关闭?Win11大小写提示图标的关闭方法
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 1)
- 如何正确计算 Kubernetes 容器 CPU 使用率
- Longest Substring Without Repeating Characters
- Wechat official account mutual aid, open white groups, and small white newspaper groups to keep warm
- [sql] usage of self connection
猜你喜欢
随机推荐
[C Advanced] deeply explore the storage of data (in-depth analysis + interpretation of typical examples)
移位距离和假设的应用
Neo4j import CSV data error: neo4j load CSV error: couldn't load the external resource
Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
LeetCode·
在混合云中管理数据库:八个关键注意事项
Quick check of OGC WebGIS common service standards (wms/wmts/tms/wfs)
hello world驱动(二)-初级版
Anti electronic ink screen st7302
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
Execution process behind shell commands
Nahamcon CTF 2022 babyrev reverse analysis
Golang 中‘...‘的用法
朋友刚学完自动化测试就拿25Koffer,我功能测试何时才能到头?
Study notes of pytorch deep learning practice: convolutional neural network (Advanced)
Three years of software testing experience, salary has been stuck at 10K, how to improve and develop automated testing?
C language layered understanding (C language function)
STM——EXTI外部中断学习笔记
JVM memory model parsing
Jsd-2204-cool shark Mall (Management Commodity module) -day02










