让字典 按值大小排序的方法

dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
dict= sorted(dic.items(), key=lambda d:d[0])
print dict

dic = {'a':1, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
dict= sorted(dic.items(), key=lambda d:d[1], reverse = True)
print(dict)

结果

[('a', 31), ('aa', 74), ('asd', 4), ('bc', 5), ('c', 3), ('d', 0)]
[('aa', 74), ('bc', 5), ('asd', 4), ('c', 3), ('a', 1), ('d', 0)]
[Finished in 0.0s]

数组排序 https://www.cnblogs.com/kaibindirver/p/12700085.html

原文地址:https://www.cnblogs.com/kaibindirver/p/10722878.html