I modified the best solution I found:
# given an iterable of pairs return the key corresponding to the greatest value
def argmax(pairs):
return max(pairs, key=lambda x: x[1])[0]
# given an iterable of values return the index of the greatest value
def argmax_index(values):
return argmax(enumerate(values))
# given an iterable of keys and a function f, return the key with largest f(key)
def argmax_f(keys, f):
return max(keys, key=f)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…