In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "Python variable and immutable data and deep copy and shallow copy case analysis". The editor shows you the operation process through actual cases, and the operation method is simple, fast and practical. I hope this article "Python variable and immutable data and deep copy and shallow copy case analysis" can help you solve the problem.
Shallow copy and deep copy
The copy function is a function specifically used for variable data types list, set, and dict. The effect is that when one value points to another, it does not affect the pointing value, and if the data being pointed to is variable data, once it is modified, the pointing data will change accordingly.
What is mutable data and immutable data
Let's take an example. Integers are immutable data, so why immutable data? Whether a data is changeable or not is related to the caching mechanism of python.
When a data changes, if its memory address does not change, it is a variable data.
For example, we now create a variable whose value is a, whose value is 100, and then change that value to see if the memory address of each variable has changed.
A = 100print (a, id (a)) # 100 1610845392a + = 100print (a, id (a)) # 200 1610848592
We find that the value has changed, the memory of the variable has also changed, and we create another variable b, which is also an integer 100.
B = 100print (b, id (b)) # 100 1610845392
It is found that the memory address of b is the same as that of a, that is to say, for a data type such as an integer, a number monopolizes a memory address, and when a variable pointing to this value changes, it is not the value of the variable that needs to change, but the variable to find the memory address of the changed value, and then point to it again. As long as your hardware does not restart, then this memory address will never change, such data is immutable data.
Then, conversely, it is variable data, which means that when the value pointed to by the variable changes, the value on this memory address actually changes, that is, the variable data type.
For example, after the list, the list changes on the original basis, so the memory address will not change, this is the variable data type, the variable data type does not have the memory cache mechanism, cannot save memory, so the identical data, their memory address may not be the same.
A = [1,2] print (a, id (a)) # [1,2] 1528536069896a.append (3) print (a, id (a)) # [1,2,3] 152853606989Secretb and a have the same values but different memory addresses b = [1,2,3] print (b, id (b)) # [1,2,3] 1528536069832 what is the copy function for?
In our actual work, one of the operations that are often used is to define a variable whose value is directly assigned to an original variable. However, after the definition of a variable, we do not use it as a device, but to do an operation, or to do a temporary storage, then the value of the original variable has to be changed, and the problem arises. If it is immutable data, if it is mutable data, the direct assignment of their memory address is the same, if the value of a variable changes. The value of the same memory address changes, and the value we want to store temporarily is no longer the value we want, which is the result we don't want to see most of the time.
Let's take the integer as an example, the variable an is directly assigned to the variable b, and the value of the variable a b is the same, but if the value of the variable a changes, it does not affect the value of the variable b at all.
A = 100print (a, id (a)) # 100 1610845392b = aprint (b, id (b)) # 100 1610845392a + = 100print (a, id (a)) # 200 1610848592print (b, id (b)) # 100 1610845392
But this is not the case if it is variable data.
A = [1,2] print (a, id (a)) # [1,2] 2077688035080b = aprint (b, id (b)) # [1,2] 2077688035080a.append (3) print (a, id (a)) # [1,2,3] 2077688035080print (b, id (b)) # [1,2,3] 2077688035080
This characteristic of immutable data is both an advantage and a disadvantage, and the disadvantage is that if we want to save a condition before the a variable changes, it can not be saved, then there is a copy function, which can turn mutable data into immutable data.
Shallow copy
Using the copy function, putting the a variable into the function as an argument, and using the b variable to accept the return value of the function, you successfully copy the variable a, and the memory address of the variable b is different from that of the variable a, so that when one of them changes, it will not affect the data of the other.
# copy function cannot be used directly. You need to use import to import copy module. The copy function of copy module is a shallow copy of import copya = [1,2,3] # variable b is no longer a direct assignment of variable a, but through the return value of copy function b = copy.copy (a) # their values are the same, but the memory address is different, so any change between them will not affect the second party. Print (a, id (a)) # [1,2,3] 2343743813320print (b, id (b)) # [1,2,3] 2343743813192a.append (4) print (a, id (a)) # [1,2,3,4] 2343743813320print (b, id (b)) # [1,2,3] 2343743813192
However, if the variable an is a second-level container or a more multi-level container, shallow copy cannot copy the second-level container or more-level container, so when the second-level container or more-level container changes, it will still change, because shallow copy can only copy first-level containers, so the memory address of multi-level containers is still the same.
Import copya = [[66 id 88], 2, 3] b = copy.copy (a) print (a, id (a)) # [[6 6 id 88], 2, 3] 2431683163720print (b, id (b)) # [6 6cr 88], 2, 3] 243168316218 changing the secondary container a [0] .append (100) print (a, id (a) # [[66 Ma 88, 100], 2, 3] 2431683163720print (b, id (b)) # [ 3] 243168316218 shallow copy cannot copy container print (id (a [0])) # 1582481372872print (id (b [0])) # 1582481372872 deep copy
A shallow copy can only copy a first-class container
So deep copies are born, and deep copies can copy containers at all levels.
Import copya = [[66 id 88], 2, 3] # Deep copy using the deepcopy function b = copy.deepcopy (a) print (a, id (a)) # [[6 6jue 88], 2, 3] 2168411158088print (b, id (b)) # [6 line 88], 2, 3] 2168411156552a [0] .append (100) print (a, id (a)) # [6 id (a), 2, 3] 2168411158088print (b, id (b)) 3] 216841115655 Deep copy Container print (id (a [0])) # 2168411158216print (id (b [0])) # 2168411122760 on "Python variable and immutable data and Deep copy and shallow copy instance Analysis" ends here Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.