Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 二维数组中的赋值_Python - Fatal编程技术网

Python 二维数组中的赋值

Python 二维数组中的赋值,python,Python,我正在使用Python 3.5 这是我的代码: a=[[1,2,3],[4,5,6]] b=a[:][:] a[0][0]=7 print(a) # [[7, 2, 3], [4, 5, 6]] print(b) # [[7, 2, 3], [4, 5, 6]] 我需要b=[[1,2,3],[4,5,6]]。如何修复它?使用deepcopy: >>> import copy >>> b = copy.deepcopy(a) >>> a[0]

我正在使用Python 3.5

这是我的代码:

a=[[1,2,3],[4,5,6]]
b=a[:][:]
a[0][0]=7
print(a) # [[7, 2, 3], [4, 5, 6]]
print(b) # [[7, 2, 3], [4, 5, 6]]
我需要
b=[[1,2,3],[4,5,6]]
。如何修复它?

使用deepcopy:

>>> import copy
>>> b = copy.deepcopy(a)
>>> a[0][0]=7
>>> print(a)
[[7, 2, 3], [4, 5, 6]]
>>> print(b)
[[1, 2, 3], [4, 5, 6]]
请试试这个:

from copy import copy, deepcopy
b = deepcopy(a)
或者简单地说:

b = [row[:] for row in a]