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 largest f(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

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -