python lists copying is it deep copy or Shallow copy and how is it done?

耗尽温柔 提交于 2019-12-11 05:57:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!