python中的简单nxn矩阵不起作用

2024-06-16 09:31:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我用这个来创建“x”的4x4矩阵:

listof=[] #table
nic=[] #row
max = 4 #tabele size
nic = ['x']*max #row of x-es
listof = [nic]*max #table of rows
print(listof) #it looks ok
listof[1][1] ="o" #changing one x to o
print(listof) # wrong since all rows have o on index 1

什么?怎么会?你知道吗

顺便说一句:我知道如果我使用:

listof = [["x" for x in range(max)] for y in range(max)]

但是上面的代码有什么问题? 谢谢


Tags: ofinforsizeestablerange矩阵