Python循环覆盖数据

时间:2019-12-02 18:34:09

标签: python

我有以下代码:

games = rotations = 4

l = [ [ [""] * 3 ] * games ] * rotations

for i in range(rotations):
  for j in range(games):
    l[i][j][2] = j + 1

print(l)

l是

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

因此循环后应该打印

[[['', '', 1], ['', '', 2], ['', '', 3], ['', '', 4]], [['', '', 1], ['', '', 2], ['', '', 3], ['', '', 4]], [['', '', 1], ['', '', 2], ['', '', 3], ['', '', 4]], [['', '', 1], ['', '',2], ['', '', 3], ['', '', 4]]]

但它正在打印

[[['', '', 4], ['', '', 4], ['', '', 4], ['', '', 4]], [['', '', 4], ['', '', 4], ['', '', 4], ['', '', 4]], [['', '', 4], ['', '', 4], ['', '', 4], ['', '', 4]], [['', '', 4], ['', '',4], ['', '', 4], ['', '', 4]]]

为什么会这样?

谢谢

0 个答案:

没有答案