问题
How is Deep copy being done in python for lists?
I am a little confused for copying of lists. Is it using shallow copy or deep copy?
Also, what is the syntax for sublists? is it g=a[:]
?
回答1:
The new list is a copy of references. g[0]
and a[0]
both reference the same object. Thus this is a shallow copy. You can see the copy
module's deepcopy
method for recursively copying containers, but this isn't a common operation in my experience.
Stylistically, I prefer the more explicit g = list(a)
to create a copy of a list, but creating a full slice has the same effect.
回答2:
From Python Doc you have to use copy.deepcopy(x)
来源:https://stackoverflow.com/questions/14028164/python-lists-copying-is-it-deep-copy-or-shallow-copy-and-how-is-it-done