在下面的代码中,[4] [7]处的zomPos是唯一应为zomPos数组中的非零值。但是,在第7个索引处的所有行都不仅仅是28,而不仅仅是zomPos [4] [7]为28。
turn = 0
zomPos = [[0]*8]*5
zombies = [[0,4,28],[1,1,6],[2,0,10],[2,4,15],[3,2,16],[3,3,13]]
for zombie in zombies:
if zombie[0] == turn:
zomPos[zombie[1]][7] = zombie[2]
print(zomPos)
tl; dr:
代码应打印:
[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 28]]
但打印:
[[0, 0, 0, 0, 0, 0, 0, 28],
[0, 0, 0, 0, 0, 0, 0, 28],
[0, 0, 0, 0, 0, 0, 0, 28],
[0, 0, 0, 0, 0, 0, 0, 28],
[0, 0, 0, 0, 0, 0, 0, 28]]
答案 0 :(得分:0)
弄清楚了... zomPos = [[0]*8]*5
创建了5个相同的数组...
zomPos = [ [0] * 8 for t in range(5) ]
是我所需要的。