Python basics -- list assignment

Encountered the problem of list assignment, the reason is that I want to assign the value of one list-a to another list-b, and use it as an 'element' of the new array, and found that the data processing is a mess. For example: a
to
insert image description here
c After the assignment is completed, change the value of the a element, and find that the value of c has also changed.
Reason: In python, the assignment of an object is a simple object reference, which is different from C++. As above, c[1]=a means assigning the address of a to b. That is, a and c[1] share a piece of memory address.
insert image description here
You can use whether a and c[1] are the same to judge, and return True, indicating that they have the same address and the same content.
Assignment operations (including objects as parameters and return values) do not open up new memory space, it just copies the reference of the new object. In other words, there is no memory overhead other than the name c. Modifying a will affect c; similarly, modifying c will affect a.
1. Shallow copy (shallow copy)
A shallow copy will create a new object whose content is a reference to the original object. There are three types of shallow copy: slice 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. List function: b = list(a)
3. Copy function: b = copy.copy(a)

The b generated by the shallow copy is no longer a. The sentence b=a[:] first calculates the value on the right side of the equal sign, and then creates a new address, and c points to this address. Use is to find that they are not the same object, use id to view, and find that they do not point to the same piece of memory. But when we use id(x) for x in a and id(x) for x in b, we can see that the addresses of the elements they contain are the same. In this case, a and b are different objects, and modifying b should theoretically not affect a.
insert image description here
Note: The reason why shallow copy is called shallow copy is that it only copies one layer. If there is a nested list in a, the situation is different.
insert image description here
a[3].append(). Check b and find that b has also changed. This is because the nested list is modified. Modifying the outer element will modify its references, making them point to other locations, modifying the elements in the nested list, and the address of the list will not change, pointing to the same location.
2. Deep copy (deep copy)
There is only one form of deep copy, the deepcopy function in the copy module.
Corresponding to shallow copy, deep copy copies all elements of the object, including multi-level nested elements. Therefore, its time and space overhead is high.
Also for la, if b = copy.deepcopy(a) is used , modifying b will not affect a. Even if the nested list has a deeper level, it will not have any impact, because the deep copied object is simply a brand new object, which is no longer related to the original object.
3. About the copy operation
1. For non-container types, such as numbers, characters, and other "atomic" types, there is no such thing as copying. All 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, only a shallow copy can be obtained.

Guess you like

Origin blog.csdn.net/github_38060285/article/details/102848142