Last active
December 25, 2015 07:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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