【发布时间】:2019-11-29 00:58:32
【问题描述】:
我正在使用 python 中的二维列表并尝试将列表分成单元格。
for line in board:
self.row.append(line)
cell_idx = 0 # Initial resetting value
if row_idx % 3 == 0:
cell_idx += 3 # New resetting value
单元格索引为列表中的每一行重置,但我希望它在 3 次迭代后重置为 3,在 6 次迭代后重置为 6,直到包括 9。
我知道我可以使用模来隔离每三个进程,但是在运行第 4 次和第 5 次迭代而不是 0 时如何重置为 3?
编辑:在阅读了我忽略的答案后,我意识到在循环中使用时 cell_idx 会增加。我不知道为什么我没有在这里包含更多内容:
for col_idx in range(9):
self.col[col_idx].insert(row_idx, line[col_idx])
# This part needs the index
self.cell[cell_idx].insert(row_idx, line[col_idx + triple])
if col_idx % 3 == 0:
cell_idx += 1
而cell_idx如下
add triple to list (cell_idx)0: 0 1 2
add triple to list (cell_idx)1: 0 1 2
add triple to list (cell_idx)2: 0 1 2
add triple to list (cell_idx)0: 0 1 2
add triple to list (cell_idx)1: 0 1 2
add triple to list (cell_idx)2: 0 1 2
add triple to list (cell_idx)0: 0 1 2
add triple to list (cell_idx)1: 0 1 2
add triple to list (cell_idx)2: 0 1 2
add triple to list (cell_idx)3: 3 4 5
add triple to list (cell_idx)4: 3 4 5
add triple to list (cell_idx)5: 3 4 5
add triple to list (cell_idx)3: 3 4 5
add triple to list (cell_idx)4: 3 4 5
add triple to list (cell_idx)5: 3 4 5
add triple to list (cell_idx)3: 3 4 5
add triple to list (cell_idx)4: 3 4 5
add triple to list (cell_idx)5: 3 4 5
add triple to list (cell_idx)6: 6 7 8
add triple to list (cell_idx)7: 6 7 8
add triple to list (cell_idx)8: 6 7 8
add triple to list (cell_idx)6: 6 7 8
add triple to list (cell_idx)7: 6 7 8
add triple to list (cell_idx)8: 6 7 8
add triple to list (cell_idx)6: 6 7 8
add triple to list (cell_idx)7: 6 7 8
add triple to list (cell_idx)8: 6 7 8
【问题讨论】:
-
您能否为您的问题提供示例输入和输出?
-
尝试在
for循环之前移动线cell_idx。
标签: python