Sort by value
http://stackoverflow.com/questions/613183/python-sort-a-dictionary-by-valueIf you want to have descending just add reverse=True
import operator
x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True)
Sort by key
http://stackoverflow.com/questions/9001509/python-dictionary-sort-by-key
import collections
d = {2:3, 1:89, 4:5, 3:0}
od = collections.OrderedDict(sorted(d.items()))
No comments:
Post a Comment