我正在尝试找到一个返回的函数all给定列表中最大值的出现次数。
numpy.argmax http://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html但是只返回它找到的第一个匹配项。例如:
from numpy import argmax
list = [7, 6, 5, 7, 6, 7, 6, 6, 6, 4, 5, 6]
winner = argmax(list)
print winner
仅给出索引0
。但我希望它给出所有索引:0, 3, 5
.
作为文档np.argmax
says: “如果多次出现最大值,则返回与第一次出现相对应的索引。”,所以你需要另一个策略。
您的一种选择是使用np.argwhere
结合np.amax
:
>>> import numpy as np
>>> listy = [7, 6, 5, 7, 6, 7, 6, 6, 6, 4, 5, 6]
>>> winner = np.argwhere(listy == np.amax(listy))
>>> print(winner)
[[0]
[3]
[5]]
>>> print(winner.flatten().tolist()) # if you want it as a list
[0, 3, 5]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)