Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Created March 31, 2013 02:38
Show Gist options
  • Save satomacoto/5279273 to your computer and use it in GitHub Desktop.
Save satomacoto/5279273 to your computer and use it in GitHub Desktop.
Sort a dictionary by value
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)
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))
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])
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