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

reverse=True 等于从大到小

 

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

# 字典排序
a = {'a': 3, 'c': 89, 'b': 0, 'd': 34}
# 按照字典的值进行排序
a1 = sorted(a.items(), key=lambda x: x[1])
# 按照字典的键进行排序
a2 = sorted(a.items(), key=lambda x: x[0])
print('按值排序后结果', a1)
print('按键排序后结果', a2)
print('结果转为字典格式', dict(a1))
print('结果转为字典格式', dict(a2))


比较复杂的这样写
a={"刘锴傧;": {"resolved": 1,"onlineBug": 0,"bugAllSum": 4},"陈玉林;": {"in_progress": 1,"onlineBug": 0,"bugAllSum": 2}}
print(a.items())
# # 按照字典的值进行排序
a1 = sorted(a.items(), key=lambda x: x[1]["bugAllSum"])

 

posted @ 2019-04-17 13:14  凯宾斯基  阅读(1030)  评论(0编辑  收藏  举报