Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List - Fatal编程技术网

Python 列表中的列表未按预期运行

Python 列表中的列表未按预期运行,python,list,Python,List,对于我的项目,我有一个非常大的列表,但是为了这个例子,我简化了它 board = [[0] * 3] * 3 board2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 根据我的理解,上面的两个列表(board和board2)应该是相等的,尽管当我使用以下代码测试它们时,它们的行为不同 board = [[0] * 3] * 3 board2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] board[0][0] = 1 board2[0][0

对于我的项目,我有一个非常大的列表,但是为了这个例子,我简化了它

board = [[0] * 3] * 3
board2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
根据我的理解,上面的两个列表(board和board2)应该是相等的,尽管当我使用以下代码测试它们时,它们的行为不同

board = [[0] * 3] * 3
board2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
board[0][0] = 1
board2[0][0] = 1
print(board)
print(board2)
我认为这两块板应该打印相同的输出,但它们是不同的

[[1, 0, 0], [1, 0, 0], [1, 0, 0]]  # this is the output of board
[[1, 0, 0], [0, 0, 0], [0, 0, 0]]  # this is the output of board2
正如您所看到的,board2的行为与我预期的一样,尽管在board中,列表中的每个第一项都被更改为1,而不仅仅是第一个列表中的第一项