In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what is the method of Python List assignment?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the method of Python List assignment" article.
There are three types of use of object replication in Python: assignment, shallow copy and deep copy. They are not only different but also related, just recently encountered this kind of problem, study.
I. assignment
In python, object assignments are simple object references, unlike C++. As follows:
In this case, b and an are the same, they point to the same piece of memory, and b is just an alias for an and a reference. We can use whether an and b are the same or not, and return True, indicating that they have the same address and the same content.
Assignment operations (including objects as parameters, return values) do not open up new memory space, it simply copies references to new objects. That is, there is no memory overhead other than the name b.
If you modify a, you affect b; by the same token, modifying b affects A.
II. Shallow copy (shallow copy)
A shallow copy creates a new object whose content is a reference to the original object.
There are three forms of shallow copy: slicing operation, factory function, and copy function in the copy module. For example, for the above a:
1. Slicing operation: B = a [:] or b = [each for each in a]
2. Factory function: B = list (a)
3. Copy function: B = copy.copy (a)
The b produced by shallow copy is no longer a. Using is, you can find that they are not the same object. Using id to check, you can find that they do not point to the same piece of memory. But when we use id (x) for x in an and id (x) for x in b, we can see that the addresses of the elements they contain are the same.
In this case, an and b are different objects, and modifying b will not theoretically affect a. For example, b.append ([4d5]).
Note, however, that the reason why a shallow copy is called a shallow copy is that it only copies one layer, and there is a nested list in a, which would be different if we modified it.
A [4] .append ("C"). Look at b and you will see that b has also changed. This is because you modified the nested list. Modifying the outer element modifies its references so that they point to another location, modifies the elements in the nested list, and the address of the list is changed, pointing to the same location.
Deep copy (deep copy)
There is only one form of deep copy, the deepcopy function in the copy module.
Corresponding to the shallow copy, the deep copy copies all the elements of the object, including multiple layers of nested elements. Therefore, its time and space cost is high.
Also for la, if you use b = copy.deepcopy (a), modifying b will not affect a. Even if the nested list has a deeper level, it will not have any impact, because the deeply copied object is a completely new object and no longer has anything to do with the original object.
IV. Warning about copy operation
1. For non-container types, such as numbers, characters, and other "atomic" types, there is no copy. All that is generated are references to the original object.
2. If the tuple variable value contains an atomic type object, even if a deep copy is used, you can only get a shallow copy.
The above is the content of this article on "what is the method of Python List assignment". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.