itertools - implementing argmax in Python -
how should argmax implemented in python? should efficient possible, should work iterables.
three ways implemented:
- given iterable of pairs return key corresponding greatest value
- given iterable of values return index of greatest value
- given iterable of keys , function
f
, return key largestf(key)
i modified best solution found:
# given iterable of pairs return key corresponding greatest value def argmax(pairs): return max(pairs, key=lambda x: x[1])[0] # given iterable of values return index of greatest value def argmax_index(values): return argmax(enumerate(values)) # given iterable of keys , function f, return key largest f(key) def argmax_f(keys, f): return max(keys, key=f)
Comments
Post a Comment