Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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替换2D数组中的值_Python_Python 3.x - Fatal编程技术网

Python替换2D数组中的值

Python替换2D数组中的值,python,python-3.x,Python,Python 3.x,我想用从excel检索的特定值替换特定索引中的值。我将用检索到的最后一个值替换我的整个数组 details= [[""] * 2 ] * 2 details_index=3 for count1 in range(len(details)-1): details[count1][0] = sheet.cell(row=count1+2, column=details_index).value 输出总是返回 [['0009', ''], ['0009', ''], ['0009', ''

我想用从excel检索的特定值替换特定索引中的值。我将用检索到的最后一个值替换我的整个数组

details= [[""] * 2 ] * 2
details_index=3
for count1 in range(len(details)-1):
    details[count1][0] = sheet.cell(row=count1+2, column=details_index).value
输出总是返回

[['0009', ''], ['0009', ''], ['0009', ''], ['0009', ''], ['0009', ''], ['0009', ''], ['0009', ''], ['0009', ''], ['0009', ''], ['0009', '']]

知道循环做错了什么吗?

当你乘法数组时,你指的是数组的指针,而不是复制

[[]*2]*2正在制作[[,]*[,]]

但实际上你的记忆中只有[,]。因此,如果你改变其中一个,你就改变了所有的

也就是说,如果取数组[0][0]=[9,0],则取内存中的位置,并表示[,]现在是[9,0],因此数组=[[9,0],[9,0]]

解决这个问题的一种方法是使用deepcopy,您可以通过从copy导入deepcopy来使用它


我想这就是你要问的。

你有一个指向相同数据的其他列表。改为使用details=[[forcoluminrange2]代替range2]中的行。请参阅如何解决此问题。。。