Created
December 4, 2012 00:31
-
-
Save perone/4199373 to your computer and use it in GitHub Desktop.
Python max dict value
This file contains 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
import timeit | |
t = timeit.Timer("v=sorted(d.items(), key=lambda x: x[1])[-1]", | |
"d = {'a': 1000, 'b': 3000, 'c':100}") | |
print t.timeit() | |
# 1.648s | |
t = timeit.Timer("v=max(d.iteritems(), key = operator.itemgetter(1))[0]", | |
"import operator; d = {'a': 1000, 'b': 3000, 'c':100}") | |
print t.timeit() | |
# 0.759s | |
t = timeit.Timer("v=max(zip(*zip(*d.items())[::-1]))[1]", | |
"d = {'a': 1000, 'b': 3000, 'c':100}") | |
print t.timeit() | |
# 1.672s | |
t = timeit.Timer("v=max(d, key=d.get)", | |
"d = {'a': 1000, 'b': 3000, 'c':100}") | |
print t.timeit() | |
# 0.499s | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment