current position:Home>Talk about deep and shallow copying in Python

Talk about deep and shallow copying in Python

2022-02-01 02:22:58 Solitary cold

「 This is my participation 11 The fourth of the yuegengwen challenge 20 God , Check out the activity details :2021 One last more challenge

First , What we need to know is what deep and shallow replication is & What the hell is this thing for !

For example : There are sugar paper and sugar , Deep copy is equivalent to sugar paper and sugar , The only shallow copy is sugar paper .( Keep this sentence in mind , After you read this article, you can guess this sentence again and again , If you have deep experience —— So congratulations : You have a firm grasp of python Copy the depth in !)

How do we judge whether the objects before and after copying are the same ?
And that's where it comes in id(object), It returns the of the object “ ID number ”( stay c++ Represents the address in memory ), Unique and unchanging .

Out of order , use is When judging whether two objects are equal , Based on this id value . id Check the same address , It shows that they point to the same space

1. introduce ——python Assignment in =

Directly perform the assignment operation , Observe :

a = [1,2,3,4]
b = a
id(a)
id(b)    # At this time a and b Of id It's the same 
 Copy code 

Let's change the list a, Look again :

a.append(4)
id(a)
id(b)    # At this time a and b Of id It's still the same , And it hasn't changed .
 Copy code 

In this way, although you can copy , But whatever changes a still b, Final a and b Will change .

expand :
If you change its value , The memory address has not changed , So it's a mutable type . The value type is immutable , Tuples and strings are immutable , The list is variable .

2. Formal explanation python The depth of the copy in

Be careful : Deep and shallow copy specific in nested list ( It can also be used in the dictionary ) Will work in . If it is a single-layer list , Such as li = [1,2,3,4], So deep and shallow id And the original list id All different , Modify the original list , The list of deep and shallow copies will not change .( After reading the following explanation, you will deeply understand the principle of this sentence !)

(1) Shallow copy explanation :

Go straight to the code , Time is the only test of truth !!!

                                                                    # Shallow copy , adopt id() Function to find the before and after copying 
li = [1,2,3]                                                         print(id(li))       Output :190679494 1256                                                      
li2 = li.copy() 													 print(id(li2))      Output :190679493 8824 
print(li is li2)	#  Output is false 
print(li2)  	    #  Print all li list ( The two are as like as two peas. !) 
 Copy code 

Draw a conclusion through observation : Before and after replication li and li2 Of id Different ,( namely li and li2 Pointing to the computer is not the same space ), but li and li2 identical . If you change li perhaps li2, It doesn't affect the other . The essence : Did not change the position of the element in the computer , Only the surface layer can be copied , such as : Count .

 expand :         
 Words used in the project : Can reduce the amount of code , Give a sum directly li The same list ,
( Without changing the given data , Change yourself ) Running time can also be reduced ( Optimize the code )
 Copy code 

(2) Deep copy explanation :

To use deep copy , To import a module first .

 The import module :                     import copy                                                                                                     
 Use :                        copy.deepcopy
 Copy code 

Go straight to the code —— Time is the only test of truth !


import copy 
a = [1,2,3,4]                                                          
a2 = copy.deepcopy(a)                                              
print(id(a))
print(id(a2))
print(a is a2)
 Copy code 

Draw a conclusion through observation : Before and after replication a and a2 Of id Different ,a and a2 identical .

(3) Both comparisons :

Difference between the two : A shallow copy : Copied its outermost layer , Nested list , The inner layer still points to the original memory address . Deep copy : Completely copied it again , It has nothing to do with the original list . In order to bring you in-depth understanding —— The following two practical examples ( A single level list ; A nested list ) Detailed instructions :

In The End!

 Please add a picture description

From now on , Hold the line , Make a little progress every day , Near future , You will thank you for your efforts !

The blogger will continue to update the basic crawler column and the actual crawler column , My friends who have read this article carefully , You can like it and comment on it . And can pay attention to this blogger , Read more about reptiles in the days to come !

If there are mistakes or inappropriate words, please point out in the comments section , thank you !
If you reprint this article, please contact me for my consent , And indicate the source and the name of the blogger , thank you !

copyright notice
author[Solitary cold],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010222566784.html

Random recommended