标签:反序 ict print div code lam rev get res
1、利用key排序
d = {‘d1‘:2, ‘d2‘:4, ‘d4‘:1,‘d3‘:3,} for k in sorted(d): print(k,d[k])
d1 2
d2 4
d3 3
d4 1
2、利用value排序:__getitem__
d = {‘d1‘:2, ‘d2‘:4, ‘d4‘:1,‘d3‘:3,} for k in sorted(d,key=d.__getitem__): print(k,d[k])
d4 1
d1 2
d3 3
d2 4
反序:reverse=True
d = {‘d1‘:2, ‘d2‘:4, ‘d4‘:1,‘d3‘:3,} for k in sorted(d,key=d.__getitem__,reverse=True): print(k,d[k])
d2 4
d3 3
d1 2
d4 1
3、对dict_items进行排序
d = {‘d1‘:2, ‘d2‘:4, ‘d4‘:1,‘d3‘:3,} res = sorted(d.items(),key=lambda d:d[1],reverse=True) print(res)
[(‘d2‘, 4), (‘d3‘, 3), (‘d1‘, 2), (‘d4‘, 1)]
标签:反序 ict print div code lam rev get res
原文地址:https://www.cnblogs.com/kilometerwine/p/9712329.html