python - 如何在 Python 中找到最大矩阵数的索引?
问题描述
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 文档中:
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 - python - 多类逻辑回归预测季节
- vue.js - Vue:创建钩子时出错:“TypeError:无法将未定义或空值转换为对象”
- javascript - JavaScript .play() 和 .pause() 不起作用
- c# - 看似没有错误的非工作 C# 代码
- node.js - 向 Mongoose 注册模型的正确方法
- swift - 如何在 RxSwift 中观察对象的属性?
- docker - Heroku 上的 .Net Core 与 Docker
- javascript - .click 功能在页面加载时触发
- asp.net-mvc - @Html.ActionLink 可以带 id 发送,在其他视图中查看 id
- go - 如何在 NAT 后面的两个节点之间进行通信?