如何返回给定矩阵中的行数(位置)? 起初我以为我可以用 board.index(row) 来做到这一点,但在这种情况下:
board = [[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
[1,1,1,1]]
for row in board:
print board.index(row)
它返回 0 1 2 0,因为第一行和最后一行是相似的。
我该如何解决这个问题?
最佳答案
尝试:
board = [[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
[1,1,1,1]]
for i, row in enumerate(board):
print board.index(row, i)
输出:
0
1
2
3
说明:
index(value, [start, [stop]])
在我的代码中,我使用枚举来模拟循环中每个板的当前索引。然后,我要求 index
方法搜索特定的值,但从其 index 开始(枚举为我提供了)。
关于python - 确定矩阵中具有重复行的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36828502/