m - >我的矩阵
m = [[19, 17, 12], [6, 9, 3], [8, 11, 1], [18, 1, 12]]
max - >我已经找到了最多的数字
max = 19
现在我找不到索引了
for i in range(len(m)): for c in m[i]: if c==19: print(m.index(c))
我收到了一个错误
Traceback (most recent call last): File "<pyshell#97>", line 4, in <module> print(m.index(c)) ValueError: 19 is not in list
我怎么处理这个?
解决方案:
从我个人的“备忘单”,或“HS-nebula”提出的numpy docs:
import numpy as np mat = np.array([[1.3,3.4,0.1],[4.0,3.2,4.5]]) i, j = np.unravel_index(mat.argmax(), mat.shape) print(mat[i][j])# or the equivalent:idx = np.unravel_index(mat.argmax(), mat.shape) print(mat[idx])
未经允许不得转载:编程自学网 » 如何在Python中找到最大矩阵数的索引?