Shades of copy

Contrast of dark and light copy

list assignment

For example : the list a=['a', 'b', 'c', 'd', 1, 2, 3], when executing the code b=a, you can get b=['a', 'b', ' c', 'd', 1, 2, 3], the specific implementation is as follows:

>>> a=['a', 'b', 'c', 'd', 1, 2, 3]
>>> b=a
>>> id(a),id(b)
(2805890045000, 2805890045000)

It can be seen that the memory addresses of the two lists a and b are the same, so what happens to the b list when we change an element in the list a?

>>> a[0]='jersey'
>>> a
['jersey', 'b', 'c', 'd', 1, 2, 3]
>>> b
['jersey', 'b', 'c', 'd', 1, 2, 3]
>>> id(a),id(a[0]),id(b),id(b[0])
(2805890045000, 2805890018920, 2805890045000, 2805890018920)

We can see that id(a) and id(b) are the same, and so are id(a[0]) and id(b[0]). This means that when we point the variable b to the a list, the b list and the a list share the same memory address, then when the elements in the list a change, the elements in the list b that share the memory address also occur at the same time Variety.

Shallow copy

So is there a way, like a character variable, when the first variable is changed, the second variable is not affected? As shown in the following program, when the value of a is assigned to b, changing the values ​​of a and b will not have any effect:

>>> a=1
>>> b=a
>>> b
1
>>> a=2
>>> a,b
(2, 1)

Here, we need to understand why this is so? In fact, assignment does not mean that the variable is changed. we know:

  1. a=1, the computer actually opened up a memory address, put 1 in, and let the variable point to the memory address of the 1;
  2. b=a, in fact, the variable b points to the memory address of 1 at the same time;
  3. When the third step a=2 is reached, the computer re-opens a new memory address, puts 2 into it, and lets a point to this new memory address. However, the memory address pointed to by b is still the memory address of 1, so nothing has changed.

In the same way, if we implement the function of changing an element in the list, we actually need to make the memory addresses of the two lists different , so they will not interfere with each other when modifying. This is where the .copy() operator can be introduced . As shown in the following code:

>>> a=['a', 'b', 'c', 'd', 1, 2, 3]
>>> b=a.copy()
>>> b
['a', 'b', 'c', 'd', 1, 2, 3]
>>> a[0]='A'
>>> a
['A', 'b', 'c', 'd', 1, 2, 3]
>>> b
['a', 'b', 'c', 'd', 1, 2, 3]

It is not difficult to find that when the value of the first element in the a list is changed, the b list does not change at all. This is because through the .copy() operator, the computer actually opens up a new id and puts the a list Assign the value to this new id, and let the variable b point to this id, then when the elements in a are changed, it has nothing to do with the b list~

deep copy

So what happens when we nest a new list inside a list? Let's take a look at the code implementation first~

>>> a = ['alex', 'shanshan', 24, ['longting', 22]]
>>> b=a.copy()
>>> b
['alex', 'shanshan', 24, ['longting', 22]]
>>> a[3][0]='jersey'
>>> a
['alex', 'shanshan', 24, ['jersey', 22]]
>>> b
['alex', 'shanshan', 24, ['jersey', 22]]

We can see that when the elements in the nested new list are changed, the b list that should not be changed in principle, has changed at the same time ~ why is this?
Through the analysis in our shallow copy above , if the two lists point to the same memory address, then when one list changes, the other one will change, so now let's verify these two nested The id of the new list~

>>> id(a[3]),id(b[3])
(2805890044744, 2805890044744)

We found that, as we expected, their two nested new lists point to the same memory address. This shows that when the outermost list (a in this example) is copied, it just opens up a new memory address, copies the current list into it, and makes the new variable (b in this example) point to this memory address. However, no new memory addresses are allocated for the nested lists, so the nested lists in the two lists still share the same address, so when an element in one list changes, the other changes at the same time.

At this time, the concept of deep copying needs to be introduced: that is, when copying, together with the nesting inside, a new memory address is also opened for copying. The two lists are completely independent and do not interfere with each other. The two do not change. But it is not recommended to do this , because it is extremely memory-intensive! ! ~~The opcode is: copy.deepcopy(list name)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563914&siteId=291194637