Skip to content

Instantly share code, notes, and snippets.

@mittenchops
Last active December 25, 2015 07:19
Show Gist options
  • Save mittenchops/6938764 to your computer and use it in GitHub Desktop.
Save mittenchops/6938764 to your computer and use it in GitHub Desktop.
argmax function: return the entry from list X where the argument arg is at a maximum.
def argmax(arg, X):
"""
>>> zee = [{"cool":{"stuff":1,"things":0.5}},{"cool":{"stuff":2,"things":0.25}}]
>>> argmax("cool.stuff",zee)
{'cool': {'things': 0.25, 'stuff': 2}}
>>> argmax("cool.things",zee)
{'cool': {'things': 0.5, 'stuff': 1}}
"""
ranked = sorted(X, key=lambda x: -getByDot(x,arg))
leader = filter(lambda x: getByDot(x,arg) == reduce(max,[getByDot(r, arg) for r in ranked]),ranked)[0]
return(leader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment