E0_copy = list(E0)后,我猜E0_copy是E0的深度拷贝,因为id(E0)不等于id(E0_copy)。然后我在循环中修改了E0_copy,但是为什么E0之后不一样了?

E0 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for k in range(3):
    E0_copy = list(E0)
    E0_copy[k][k] = 0
    #print(E0_copy)
print E0  # -> [[0, 2, 3], [4, 0, 6], [7, 8, 0]]

当前回答

如果列表的内容是基本数据类型,则可以使用理解式

new_list = [i for i in old_list]

你可以为多维列表嵌套它,比如:

new_grid = [[i for i in row] for row in grid]

其他回答

只是一个递归的深度复制函数。

def deepcopy(A):
    rt = []
    for elem in A:
        if isinstance(elem,list):
            rt.append(deepcopy(elem))
        else:
            rt.append(elem)
    return rt

编辑:正如Cfreak提到的,这已经在复制模块中实现了。

如果列表的内容是基本数据类型,则可以使用理解式

new_list = [i for i in old_list]

你可以为多维列表嵌套它,比如:

new_grid = [[i for i in row] for row in grid]

在Python中,有一个名为copy的模块,它有两个有用的函数:

import copy
copy.copy()
copy.deepcopy()

Copy()是一个浅拷贝函数。如果给定的参数是一个复合数据结构,例如一个列表,那么Python将创建另一个相同类型的对象(在这种情况下,是一个新列表),但对于旧列表中的所有内容,只有它们的引用被复制。可以这样想:

newList = [elem for elem in oldlist]

直观地,我们可以假设deepcopy()将遵循相同的范例,唯一的区别是,对于每个elem,我们将递归地调用deepcopy,(就像mbguy的答案一样)

但这是错误的!

Deepcopy()实际上保留了原始复合数据的图形结构:

a = [1,2]
b = [a,a] # there's only 1 object a
c = deepcopy(b)

# check the result
c[0] is a # False, a new object a_1 is created
c[0] is c[1] # True, c is [a_1, a_1] not [a_1, a_2]

这是棘手的部分:在deepcopy()过程中,使用哈希表(Python中的字典)将每个旧对象ref映射到每个新对象ref,这防止了不必要的重复,从而保留了复制的复合数据的结构。

官方文档

如果你使用deepcopy给同一个列表赋值, 请使用临时变量。由于某些原因,copy.deepcopy()在尝试使用索引自我更新同一变量时,在对象列表上不起作用

S = S[idx] ->x
S = copy.deepcopy(S[idx])  -> x

VVVV,虽然这工作

Stemp = np.zeros(N,dtype=object)
for ii in range(N):
   Stemp[ii]=copy.deepcopy(S[idx[ii]])
S= copy.deepcopy(Stemp)

@Sukrit卡尔拉

No.1: list(), [:], copy.copy()都是浅拷贝。如果一个对象是复合的,它们都不合适。你需要使用copy.deepcopy()。

2 . b直接= a, a和b有相同的参照物,改变a等于改变b。

将a设为b

如果直接将a赋给b,则a和b共享一个引用。

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = a
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0] = 1
>>> a
[1, [4, 5, 6]]
>>> b
[1, [4, 5, 6]]


>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = a
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0][1] = 10
>>> a
[[1, 10, 3], [4, 5, 6]]
>>> b
[[1, 10, 3], [4, 5, 6]]

影子复制

通过列表()

List()和[:]是相同的。除了第一层更改外,其他所有层的更改都将被传输。

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = list(a)
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0] = 1
>>> a
[1, [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]


>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = list(a)
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0][1] = 10
>>> a
[[1, 10, 3], [4, 5, 6]]
>>> b
[[1, 10, 3], [4, 5, 6]]

由[:]

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = a[:]
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0] = 1
>>> a
[1, [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]


>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = a[:]
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0][1] = 10
>>> a
[[1, 10, 3], [4, 5, 6]]
>>> b
[[1, 10, 3], [4, 5, 6]]

List()和[:]改变除了第一层之外的其他层

# =========== [:] ===========
>>> a = [[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b = a[:]
>>> a
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> a[0][2] = 4
>>> a
[[1, 2, 4], [4, 5, 6]]
>>> b
[[1, 2, 4], [4, 5, 6]]


>>> a = [[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b = a[:]
>>> a
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> a[0][2][0] = 999
>>> a
[[1, 2, [999, 6]], [4, 5, 6]]
>>> b
[[1, 2, [999, 6]], [4, 5, 6]]



# =========== list() ===========
>>> a = [[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b = list(a)
>>> a
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> a[0][2] = 4
>>> a
[[1, 2, 4], [4, 5, 6]]
>>> b
[[1, 2, 4], [4, 5, 6]]


>>> a = [[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b = list(a)
>>> a
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> b
[[1, 2, [3.5, 6]], [4, 5, 6]]
>>> a[0][2][0] = 999
>>> a
[[1, 2, [999, 6]], [4, 5, 6]]
>>> b
[[1, 2, [999, 6]], [4, 5, 6]]

通过复制()

你会发现copy()函数与list()和[:]相同。他们都是肤浅的拷贝。

有关浅复制和深度复制的更多信息,可以参考这里。

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = copy.copy(a)
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0][1] = 10
>>> a
[[1, 10, 3], [4, 5, 6]]
>>> b
[[1, 10, 3], [4, 5, 6]]

通过深度复制()

>>> import copy
>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = copy.deepcopy(a)
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0] = 1
>>> a
[1, [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]


>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = copy.deepcopy(a)
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]
>>> a[0][1] = 10
>>> a
[[1, 10, 3], [4, 5, 6]]
>>> b
[[1, 2, 3], [4, 5, 6]]