我偶尔会在Python代码中看到使用列表切片语法,就像这样:
newList = oldList[:]
这肯定就跟这个一样:
newList = oldList
还有什么我没注意到的吗?
我偶尔会在Python代码中看到使用列表切片语法,就像这样:
newList = oldList[:]
这肯定就跟这个一样:
newList = oldList
还有什么我没注意到的吗?
[:]
浅复制列表,复制包含对原始列表成员的引用的列表结构。这意味着对副本的操作不会影响原始结构。但是,如果您对列表成员进行了某些操作,则两个列表仍然引用它们,因此如果通过原始访问成员,则更新将显示出来。
深复制将复制所有列表成员。
下面的代码片段展示了浅复制的实现。
# ================================================================
# === ShallowCopy.py =============================================
# ================================================================
#
class Foo:
def __init__(self, data):
self._data = data
aa = Foo ('aaa')
bb = Foo ('bbb')
# The initial list has two elements containing 'aaa' and 'bbb'
OldList = [aa,bb]
print OldList[0]._data
# The shallow copy makes a new list pointing to the old elements
NewList = OldList[:]
print NewList[0]._data
# Updating one of the elements through the new list sees the
# change reflected when you access that element through the
# old list.
NewList[0]._data = 'xxx'
print OldList[0]._data
# Updating the new list to point to something new is not reflected
# in the old list.
NewList[0] = Foo ('ccc')
print NewList[0]._data
print OldList[0]._data
>>> # ================================================================
... # === ShallowCopy.py =============================================
... # ================================================================
... #
... class Foo:
... def __init__(self, data):
... self._data = data
...
>>> aa = Foo ('aaa')
>>> bb = Foo ('bbb')
>>>
>>> # The initial list has two elements containing 'aaa' and 'bbb'
... OldList = [aa,bb]
>>> print OldList[0]._data
aaa
>>>
>>> # The shallow copy makes a new list pointing to the old elements
... NewList = OldList[:]
>>> print NewList[0]._data
aaa
>>>
>>> # Updating one of the elements through the new list sees the
... # change reflected when you access that element through the
... # old list.
... NewList[0]._data = 'xxx'
>>> print OldList[0]._data
xxx
>>>
>>> # Updating the new list to point to something new is not reflected
... # in the old list.
... NewList[0] = Foo ('ccc')
>>> print NewList[0]._data
ccc
>>> print OldList[0]._data
xxx
就像NXC所说的那样, Python变量名实际上指向一个对象,而不是内存中的特定位置。
newList = oldList
将创建两个不同的变量,它们都指向同一个对象,因此更改oldList
将同时更改newList
。
然而,当你执行newList = oldList[:]
时,它会“切片”列表并创建一个新列表。 [:]
的默认值为0和列表的结尾,因此它复制所有内容。 因此,它创建了一个包含第一个列表中所有数据的新列表,但可以更改这两个列表而不影响另一个。
正如已经回答的那样,我将简单地添加一个演示:
>>> a = [1, 2, 3, 4]
>>> b = a
>>> c = a[:]
>>> b[2] = 10
>>> c[3] = 20
>>> a
[1, 2, 10, 4]
>>> b
[1, 2, 10, 4]
>>> c
[1, 2, 3, 20]
def myfunction(mylist=[]):
pass
...并不完全按照你想象的那样运作。
如果你有C语言背景:'='左边剩下的部分始终是一个指针。所有变量始终都是指针。如果你将变量放在一个列表中:a = [b, c],那么你就把指向b和c所指向的值的指针放在了一个由a指向的列表中。如果你随后设置a[0] = d,则位于位置0的指针现在指向d所指向的任何内容。
另请参阅copy模块:http://docs.python.org/library/copy.html
浅拷贝:(从一个位置复制一些内存块到另一个位置)
a = ['one','two','three']
b = a[:]
b[1] = 2
print id(a), a #Output: 1077248300 ['one', 'two', 'three']
print id(b), b #Output: 1077248908 ['one', 2, 'three']
深拷贝:(复制对象引用)
a = ['one','two','three']
b = a
b[1] = 2
print id(a), a #Output: 1077248300 ['one', 2, 'three']
print id(b), b #Output: 1077248300 ['one', 2, 'three']
aList[:]
为什么不可靠? - jonrsharpe