我用这个来创建“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)]
但是上面的代码有什么问题? 谢谢
问题是
listof
最终由同一列表的四个引用组成。因此,当您更改一行中的元素时,它将更改所有行中的元素。你知道吗相关问题 更多 >
编程相关推荐