“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
First, we need to understand what depth copying is & what it does!
For example, if you have sugar wrappers and sugar, a deep copy will have both, while a light copy will have only sugar wrappers. (Keep that in mind, and think about it again after you’ve read this article. If it makes sense — congratulations: you’ve mastered deep copying in Python!)
How can we tell if the object is the same before and after replication? This requires the id(object), which returns the object's "id number" (which in c++ stands for its address in memory), unique and unchanging. In addition, this id value is used to determine whether two objects are equal. Id looks at the same address, which means they're pointing to the same spaceCopy the code
1. Introduction — Assignment = in Python
Perform assignment directly and observe:
a = [1.2.3.4]
b = a
id(a)
id(b) A and B have the same id
Copy the code
Let’s change list A and see:
a.append(4)
id(a)
id(b) A and B have the same id as each other.
Copy the code
You can copy it, but if you change a or B, eventually a and B will change.
Extension: If you change its value and the memory address does not change, then it is mutable. Numeric types are immutable, tuples and strings are immutable, lists are mutable.Copy the code
2. A formal introduction to deep and shallow copying in Python
Note: Depth copying only works in nested lists (dictionaries can also be used). If it is a single list, such as li = [1,2,3,4], then the id of the depth copy is different from the id of the original list. If you modify the original list, the depth copy will not change the list. (Read the explanation below and you will understand how this sentence works!)
(1) Shallow replication explanation:
Directly on the code, time is the only standard to test the truth!!
For shallow copy, use the id() function to find before and after the copy
li = [1.2.3] print(id(li)) output:190679494 1256
li2 = li.copy() print(id(li2)) output:190679493 8824
print(li is li2) # Output false
print(li2) # Print the entire li list (they're identical!)
Copy the code
Through observation, it is concluded that the ids of Li and li2 before and after replication are different (that is, Li and li2 point to different space inside the computer), but Li and li2 are the same. If you change Li or li2, it doesn’t affect the other one. Essence: only the surface layer can be copied without changing the location of the element in the computer, such as: number.
Extension: if used in the project: can reduce the amount of code, directly give a list like Li, (without changing the given data, make the change yourself) can also reduce the running time (optimize the code)Copy the code
(2) Deep replication explanation:
To use deep replication, import modules first.
Import module:importCopy: copy. DeepcopyCopy the code
Directly on the code – time is the only standard of truth!
import copy
a = [1.2.3.4]
a2 = copy.deepcopy(a)
print(id(a))
print(id(a2))
print(a is a2)
Copy the code
The results show that the ids of a and A2 before and after replication are different, but a and A2 are the same.
(3) Comparison of the two:
The differences are as follows: shallow copy: copies its outermost layer, nesting the list, and the inner layer points to the original memory address. Deep copy: A complete copy of the original list, unrelated to the original list. To get you started — here are two practical examples (a single-tier list; A nested list)
3. In-depth explanation of the depth of replication —
(1) Single-layer list:
Nonsense not to say – directly on the code, time is the only standard to test the truth!!
import copy
li = [1.2.3]
li1 = copy.copy(li)
li2 = copy.deepcopy(li)
Copy the code
Observation: Li, li1 and li2 are exactly the same; But the id looks at different addresses, so if you append(5) after li, the lists li1 and li2 remain the same for both shallow and deep replicates.
If the list is not nested, it makes no difference. So introduce nested lists……
(2) Nested list:
First you need to know what a nested list looks like and what each layer refers to: li = [1.2.3.4[5.6[7.8There are three levels of nesting in the list li. [1.2.3.4] : the first layer of nesting (i.e. the outermost layer) [5.6] : layer 2 nesting [7.8] : The third layer of nestingCopy the code
Nonsense not to say – directly on the code, time is the only standard to test the truth!!
li = [[1.2], [3.4]]
li2 = li # li2 is assignment
li3 = copy.copy(li) #li3 is a shallow copy
li4 = copy.deepcopy(li) #li4 is a deep copy
[[1,2],[3,4]]
Copy the code
Let’s look at the ids of these four lists:
idOutput to (li)2011597308424
id(li2) output is2011597308424
id(li3) output is2011597308360
id(li4) output is2011598772872
The id of the copy li2 is the same as the id of the original list li. The id of the deep copy and shallow copy are different from the id of the original list Li
Copy the code
If you now add a number to the end of the original list, the ids of all four lists will not change
So let’s think about it a little differently.
id(li[0]) output:2011597305032
id(li2[0]) output:2011597305032
id(li3[0]) output:2011597305032
id(li4[0]) output:2011598772936
Copy the code
To sum up — come to the conclusion that:
In the nested list, the shallow copy points to the original memory address and the deep copy points to the new memory space address.
If it’s not a nested list, the depth copy points to the new space.
Deep copy to note: nested list and not nested list cases!!
Knowledge supply station memory space, add elements, and add, if the memory space address is not the same, you add and I have nothing to do!Copy the code
Add an element to the first level of nesting in the original list. , analysis……
li[0].append(5)
Copy the code
Li, li2 and li3 are added, because the memory space address is the same; But li4 will not add, because the memory space is different!!
4. Summary of the class (get out your notebook and write it down!)
Full shallow copy: When one list is copied to another list, they are actually pointing to the same list in memory, i.e. with the same memory ID. When another list changes whichever level of the nested list, the other list is also modified accordingly. But the ids of these two lists are always the same. When a list passes through a functioncopyShallow copy to another list, in fact they only the outermost layer in memory is fully deep copy, the rest of the layers are shallow copy. That is, the memory ID copies one part of it, and the other part of the ID is regenerated. When one of the lists changes only the outermost layer (i.e. the first layer), the outermost layer of the other list remains unchanged. If one of the lists modifies all the layers except the outermost, the other list is also modified. 3. When a list is deeply copied to another list through the deepcopy() function, they are actually pointing to a completely different list in memory. One of the lists is changed, and the other list is not changed at all.Copy the code
Python memory pools! a =1
b = 1Id (a) output:1402582080Id (b) output:1402582080Python has a built-in memory pool. Small values are defined in the pool, so when we create, we directly point to the memory space in the pool.Copy the code