一、对字典的值大小进行排序
from random import randint
d = {x: randint(60, 100) for x in 'xyzabcd'}
'''第一种方法使用zip()方法将字典转化为元组'''
d2 = zip(d.itervalues(), d.iterkeys())
print sorted(d2)
'''第二种方法sorted的key参数'''
d3 = d.items()
print sorted(d3, key= lambda x:x[1])
二、在多个字典中找公共键
from random import randint,sample
s1 = {x: randint(1,4) for x in sample('abcdefg',randint(3, 6))}
s2 = {x: randint(1,4) for x in sample('abcdefg',randint(3, 6))}
s3 = {x: randint(1,4) for x in sample('abcdefg',randint(3, 6))}
'''第一种方法'''
result = s1.viewkeys() & s2.viewkeys() & s3.viewkeys()
'''第二种方法:map()和reduce()'''
r = map(dict.viewkeys, [s1, s2, s3])
result = reduce(lambda a, b: a & b, r)
三、保持字典有序
使用collections中的OrderedDict()
from collections import OrderedDict
d = OrderedDict()
d['lim'] = (1, 39)
d['lin'] = (2, 55)
d['li'] = (3, 60)
for k in d:
print k