1

我有一个名为 MyList 的列表。我想将列表复制到新列表,然后将项目添加到新列表,所以我这样做:

MySecondList=MyList

for item in MyList:
    if item==2:
        MySecondList.append(item)

我遇到的问题是这些项目也将被添加到 MyList 中,事实上,循环也一直在通过 MyList 新项目!

这正常吗?为什么会这样?迭代不应该只使用原始列表 MyList ,而不是随着我添加到其他列表的项目而动态增加吗?

4

2 回答 2

1

是的,这是正常的,因为列表在 python 中是可变的并且这个操作:

MySecondList = MyList

只需创建对同一列表对象的新引用并list.append就地修改同一对象。(其他操作如+=,list.extendlist.pop也会就地修改列表)

您可以在此处使用浅拷贝:

MySecondList = MyList[:]

演示:

>>> from sys import getrefcount
>>> lis = [1,2,3]
>>> foo = lis       #creates a new reference to the same object [1,2,3]
>>> lis is foo
True
>>> getrefcount(lis) #number of references to the same object
3                    #foo , lis and shell itself

#you can modify the list [1,2,3] from any of it's references
>>> foo.append(4)
>>> lis.append(5)
>>> foo,lis
([1, 2, 3, 4, 5], [1, 2, 3, 4, 5])

>>> lis = [1,2,3]
>>> foo = lis[:]    #assigns a shallow copy of lis to foo
>>> foo is lis
False
>>> getrefcount(lis) #still 2(lis + shell_, as foo points to a different object
2

#different results here
>>> foo.append(4)
>>> lis.append(5)
>>> foo, lis
([1, 2, 3, 4], [1, 2, 3, 5])

对于列表列表(或可变对象列表),浅拷贝是不够的,因为内部列表(或对象)只是对同一对象的新引用:

>>> lis = [[1,2,3],[4,5,6]]
>>> foo = lis[:]
>>> foo is lis              #lis and foo are different
False

>>> [id(x) for x in lis]    #but inner lists are still same 
[3056076428L, 3056076716L]
>>> [id(x) for x in foo]    #same IDs of inner lists, i.e foo[0] is lis[0] == True
[3056076428L, 3056076716L]
>>> foo[0][0] = 100         # modifying one will affect the other as well
>>> lis[0],foo[0]
([100, 2, 3], [100, 2, 3])

对于这种情况,请使用copy.deepcopy

>>> from copy import deepcopy
>>> lis = [[1,2,3],[4,5,6]]
>>> foo = deepcopy(lis)
于 2013-06-22T12:16:41.287 回答
0

因为它们都引用了相同的列表(并且它们的 id 相同)。观察:

>>> a = [1,2,3]
>>> b = a
>>> b
[1, 2, 3]
>>> a is b
True
>>> b += [1]
>>> b
[1, 2, 3, 1]
>>> a
[1, 2, 3, 1]
>>> a is b
True

改为这样做:

MySecondList = MyList[:]

这样做是制作一个不会更改原始列表的列表的副本。您也可以使用list(MyList).

于 2013-06-22T12:15:19.413 回答