This question already has an answer here:
这个问题已经有了答案:
- Copying nested lists in Python 2 answers
- 在Python 2中复制嵌套列表
- How to clone or copy a list? 18 answers
- 如何克隆或复制列表?18个答案
X
is a 2D array. I want to have a new variable Y
that which has the same value as the array X
. Moreover, any further manipulations with Y should not influence the value of the X.
X是一个二维数组。我想要一个新的变量Y它的值和数组X相同,而且,任何对Y的进一步操作都不应该影响X的值。
It seems to me so natural to use y = x
. But it does not work with arrays. If I do it this way and then changes y, the x will be changed too. I found out that the problem can be solved like that: y = x[:]
在我看来,使用y = x是很自然的事,但它不适用于数组。如果我这样做,然后改变y, x也会改变。我发现问题可以这样解决:y = x[:]
But it does not work with 2D array. For example:
但它不能处理2D数组。例如:
x = [[1,2],[3,4]]
y = x[:]
y[0][0]= 1000
print x
returns [ [1000, 2], [3, 4] ]
. It also does not help if I replace y=x[:]
by y = x[:][:]
.
返回[[1000,2],[3,4]]。如果我把y=x[:]换成y=x[:]也没有用。
Does anybody know what is a proper and simple way to do it?
有人知道什么是合适的简单方法吗?
5 个解决方案
#1
37
Try this:
试试这个:
from copy import copy, deepcopy
y = deepcopy(x)
I'm not sure, maybe copy()
is sufficient.
我不确定,也许拷贝()就足够了。
#2
51
Using deepcopy() or copy() is a good solution. For a simple 2D-array case
使用deepcopy()或copy()是一个很好的解决方案。对于一个简单的2d数组情况
y = [row[:] for row in x]
#3
7
For 2D arrays it's possible use map function:
对于2D数组,可以使用map函数:
old_array = [[2, 3], [4, 5]]
# python2.*
new_array = map(list, old_array)
# python3.*
new_array = list(map(list, old_array))
#4
1
In your case(since you use list of lists) you have to use deepcopy, because 'The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.'
在你的情况中(因为你使用列表的列表)你必须使用deepcopy,因为“浅和深复制的区别仅仅是相关的复合对象(对象包含其他对象,如列表或类实例):浅拷贝构造一个新的复合对象,然后(尽可能)将引用插入对象中找到原始。深度复制构造一个新的复合对象,然后递归地将原始对象的副本插入其中。
Note that sample below is simply intended to show you an example(don't beat me to much) how deepcopy could be implemented for 1d and 2d arrays:
请注意,下面的示例只是想向您展示一个示例(不要打败我),deepcopy如何实现1d和2d数组:
arr = [[1,2],[3,4]]
deepcopy1d2d = lambda lVals: [x if not isinstance(x, list) else x[:] for x in lVals]
dst = deepcopy1d2d(arr)
dst[1][1]=150
print dst
print arr
#5
0
I think np.tile also might be useful
我认为np。瓷砖也可能有用
>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
[[0, 1, 2, 0, 1, 2]]])
#1
37
Try this:
试试这个:
from copy import copy, deepcopy
y = deepcopy(x)
I'm not sure, maybe copy()
is sufficient.
我不确定,也许拷贝()就足够了。
#2
51
Using deepcopy() or copy() is a good solution. For a simple 2D-array case
使用deepcopy()或copy()是一个很好的解决方案。对于一个简单的2d数组情况
y = [row[:] for row in x]
#3
7
For 2D arrays it's possible use map function:
对于2D数组,可以使用map函数:
old_array = [[2, 3], [4, 5]]
# python2.*
new_array = map(list, old_array)
# python3.*
new_array = list(map(list, old_array))
#4
1
In your case(since you use list of lists) you have to use deepcopy, because 'The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.'
在你的情况中(因为你使用列表的列表)你必须使用deepcopy,因为“浅和深复制的区别仅仅是相关的复合对象(对象包含其他对象,如列表或类实例):浅拷贝构造一个新的复合对象,然后(尽可能)将引用插入对象中找到原始。深度复制构造一个新的复合对象,然后递归地将原始对象的副本插入其中。
Note that sample below is simply intended to show you an example(don't beat me to much) how deepcopy could be implemented for 1d and 2d arrays:
请注意,下面的示例只是想向您展示一个示例(不要打败我),deepcopy如何实现1d和2d数组:
arr = [[1,2],[3,4]]
deepcopy1d2d = lambda lVals: [x if not isinstance(x, list) else x[:] for x in lVals]
dst = deepcopy1d2d(arr)
dst[1][1]=150
print dst
print arr
#5
0
I think np.tile also might be useful
我认为np。瓷砖也可能有用
>>> a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
[0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
[[0, 1, 2, 0, 1, 2]]])