Difference in python copy of the deepcopy

First attach the code:

import copy
x = [1, [2, 3]]
y = x.copy()
z = copy.deepcopy(y)
x[0] = 4
x[1][0] = 5
print(y)
print(z)

Output:

[1, [5, 3]]
[1, [2, 3]]

Visible, the sequence of copymethod can only achieve replication sequence neutron sequence of elements still reference.

The copymodule deepcopyis simultaneously realized and replication elements of sequence, the sequence can be used on demand when copying two ways.

Released four original articles · won praise 0 · Views 24

Guess you like

Origin blog.csdn.net/m0_46396257/article/details/104649896