根据字典的值大小对字典的项排序
from random import randint
{x: ranin(60,100) for x in 'xyzabc'}
# {'a':97,'b':69,'c':78,'x':66,'y':95,'z':72}
sorted(d)
# ['a','b','c','x','y','z']
iter(d)
# dictionary-keyiterator at 0x7f030d0d1f18
list(iter(d))
['a','c','b','y','x','z']
(97,'a') > (69,'b')
True
(97,'a') > (97,'b')
False
d.keys()
['a','c','b','y','x','z']
d.values()
[85,94,88,96,85,84]
zip(d.values(),d.keys())
[(85,'a'),(94,'c'),(88,'b'),(96,'y'),(85,'x'),(84,'z')]
# 下面这个省空间
zip(d.itervalues(),d.iterkeys())
[(85,'a'),(94,'c'),(88,'b'),(96,'y'),(85,'x'),(84,'z')]
sorted(zip(d.itervalues(),d.iterkeys()))
[(84,'z'),(85,'a'),(85,'x'),(88,'b'),(94,'c'),(96,'y')]
# 方法二
d.items()
[('a',85),('c',4),('b',88,('y',96),('x',85),('z',84)]
sorted(d.items(),key = lambda x:x[1])
[('z',84),('a',85),('x',85),('b',88),('c',94),('y',96)]