我偶尔看到在Python代码中使用这样的列表的片段语法:

newList = oldList[:]

当然这仅仅是相同:

newList = oldList

还是我失去了一些东西?

有帮助吗?

解决方案

像NXC说,Python变量名实际上指向一个对象,而不是一个特定的点在存储器中。

newList = oldList将创建指向同一对象的两个不同的变量,因此,改变oldList也会改变newList

然而,当你做newList = oldList[:],这“片”的名单,并创建一个新的列表。对于[:]的默认值是0和列表的末尾,所以它的副本应有尽有。因此,它创建与包含在第一个中的所有数据的新列表,但两者可在不改变其他改变。

其他提示

[:] href="http://en.wikipedia.org/wiki/Deep_copy#Shallow_copy" rel="noreferrer">浅拷贝的列表中,制备含有以引用列表结构的副本原来的列表成员。这意味着,在复制操作不影响原有的结构。但是,如果你做的东西到列表中的成员,这两个列表仍然提到他们,这样的更新将显示,如果成员是通过原来的访问。

一个深复制会使所有的成员以及副本。

下面的代码段示出了在作用浅表副本。

# ================================================================
# === 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

在Python壳运行它提供了以下转录。我们可以看到 名单正在与老对象的拷贝制作。一个对象可以有 其状态通过引用过旧列表更新,并将更新可 当对象是通过旧列表访问看到。最后,改变 在新的列表引用可以看出,在旧列表没有反映,作为 新列表现在参照不同的对象。

>>> # ================================================================
... # === 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

因为它已经回答了,我只需要添加一个简单的例子:

>>> 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]

从不认为这是A = B'在Python的意思是 '拷贝B到A'。如果有双方的变量,你不能真正知道。相反,认为它是“给b中的附加名称的”

如果b为不可变的对象(比如一个数字,元组或字符串),然后是,效果是,你得到一个拷贝。但是,这是因为当你处理immutables(这也许应该被称为的只读不变的或 WORM 的),你的总是得到一份拷贝,按照定义。

如果b是可变的,你的总要做些额外的事情,以确保你有一个真正的副本的。 始终。使用列表,这是简单的切片:A = B [:]。

可变性也是原因,这样的:

def myfunction(mylist=[]): 
    pass

...并不完全做你认为它。

如果您从C-背景是:还剩下些什么的“=”是一个指针,始终。所有变量都是指针,始终。如果你把变量列表:A = [B,C],你已经把指针值指向B和C在列表中被指向。如果然后设置一个[0] = d,在位置0的指针现在指向任何d点。

又见复制模块: 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']
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top