python字典根据值去排序

       日常开发中我们需要对字典根据健排序或者根据字典的值排序,可以通过python的zip()方法去实现。

根据健去排序:
a = {
    
    
    'a':1,
    'b':2,
    'c':3,
    'd':4,
}
print( sorted(zip(a.keys(),a.values())))
结果:
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
根据值去排序:
a = {
    
    
    'a':1,
    'b':2,
    'c':3,
    'd':4,
}
print(sorted(zip(a.values(),a.keys())))
结果:
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
求字典中值最大的数据:
a = {
    
    
    'a':1,
    'b':2,
    'c':3,
    'd':4,
}
print(max(zip(a.values(),a.keys())))
结果
(4, 'd')
求字典中值最小的数据:
a = {
    
    
    'a':1,
    'b':2,
    'c':3,
    'd':4,
}
print(min(zip(a.values(),a.keys())))
结果:
(1, 'a')

猜你喜欢

转载自blog.csdn.net/weixin_43697214/article/details/107332893