【问题标题】:Increasing a resetting value every nth iteration in python在python中每第n次迭代增加一个重置值
【发布时间】: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


【解决方案1】:

您可以使用整数除法。我认为这比有条件地更改附加计数器的状态要干净一些。

for i in range(12):
    cell_idx = i // 3
    print("{: >2}: {}".format(i, cell_idx))

输出:

 0:  0                                                                                                                          
 1:  0                                                                                                                          
 2:  0                                                                                                                          
 3:  1                                                                                                                          
 4:  1                                                                                                                          
 5:  1                                                                                                                          
 6:  2                                                                                                                          
 7:  2                                                                                                                          
 8:  2                                                                                                                          
 9:  3                                                                                                                          
10:  3                                                                                                                          
11:  3

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 2018-02-16
    • 2011-08-03
    • 2012-05-05
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多