Created
March 31, 2013 02:38
-
-
Save satomacoto/5279273 to your computer and use it in GitHub Desktop.
Sort a dictionary by value
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
import random | |
def sort_test(): | |
a = [random.random() for i in range(100)] | |
b = [random.random() for i in range(100)] | |
c = dict(zip(a, b)) | |
sorted(c, key=c.get) |
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
import random | |
import operator | |
def sort_test(): | |
a = [random.random() for i in range(100)] | |
b = [random.random() for i in range(100)] | |
c = dict(zip(a, b)) | |
sorted(c.iteritems(), key=operator.itemgetter(1)) |
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
import random | |
def sort_test(): | |
a = [random.random() for i in range(100)] | |
b = [random.random() for i in range(100)] | |
c = dict(zip(a, b)) | |
sorted(c.items(), key=lambda x:x[1]) |
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
import random | |
def sort_test(): | |
a = [random.random() for i in range(100)] | |
b = [random.random() for i in range(100)] | |
c = dict(zip(a, b)) | |
d = zip(c.values(), c.keys()) | |
sorted(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment