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 copy
method can only achieve replication sequence neutron sequence of elements still reference.
The copy
module deepcopy
is simultaneously realized and replication elements of sequence, the sequence can be used on demand when copying two ways.