python列表复制是深层复制还是浅层复制,如何完成? [英] python lists copying is it deep copy or Shallow copy and how is it done?
问题描述
Deep copy如何在python列表中完成?
How is Deep copy being done in python for lists?
我对复制列表有些困惑.使用浅拷贝还是深拷贝?
另外,子列表的语法是什么?是g=a[:]
吗?
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[:]
?
推荐答案
新列表是引用的副本. g[0]
和a[0]
都引用相同的对象.因此,这是一个浅表副本.您可以看到copy
模块的deepcopy
方法用于递归复制容器,但是根据我的经验,这不是常见的操作.
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.
通常,我更喜欢使用更明确的g = list(a)
创建列表的副本,但是创建完整切片具有相同的效果.
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.
这篇关于python列表复制是深层复制还是浅层复制,如何完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!