当前位置:网站首页>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

 Insert picture description here


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

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

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 :

 Insert picture description here

  • 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 :

 Insert picture description here

 ————————————————
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 :)

原网站

版权声明
本文为[Love to eat dog repair bags]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260302199772.html