# encoding: utf-8 import csv import collections d = {'banana':3,'apple':4,'pear':1,'orange':2} print(d) kd = collections.OrderedDict(sorted(d.items(),key = lambda t:t[0])) print('key order') print(kd) vd = collections.OrderedDict(sorted(d.items(),key = lambda t:t[1])) print('value order') print(vd) print(type(vd))
{'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2} key order OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) value order OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) <class 'collections.OrderedDict'> Process finished with exit code 0