创建板时,只需确保所有单元格都引用同一个列表。
例如看这个:
size=[3]
n_rows=3
n_cols=3
board=[
[('cell %d,%d'%(row,col),size) for col in range(n_cols)]
for row in range(n_rows)
]
print('Before:')
for row in board:
print(' '+str(row))
size[0] = 4
print('After:')
for row in board:
print(' '+str(row))
结果:
前:
[('cell 0,0', [3]), ('cell 0,1', [3]), ('cell 0,2', [3])]
[('cell 1,0', [3]), ('cell 1,1', [3]), ('cell 1,2', [3])]
[('cell 2,0', [3]), ('cell 2,1', [3]), ('cell 2,2', [3])]
后:
[('cell 0,0', [4]), ('cell 0,1', [4]), ('cell 0,2', [4])]
[('cell 1,0', [4]), ('cell 1,1', [4]), ('cell 1,2', [4])]
[('cell 2,0', [4]), ('cell 2,1', [4]), ('cell 2,2', [4])]