python dict字典,按照Value升序, key降序

按Value升序,按key降序

例子

dicts = {1:5, 2:4, 3:8, 4:9, 5:10, 6:5, 7:5}
sort_dicts = dict(sorted(dicts.items(), key = lambda x:[x[1],-x[0]]))

print(sort_dicts)
OutPUT:{2: 4, 7: 5, 6: 5, 1: 5, 3: 8, 4: 9, 5: 10}

按Value升序

dicts = {1:5, 2:4, 3:8, 4:9, 5:10, 6:5, 7:5}
sort_dicts = dict(sorted(dicts.items(), key = lambda x:[x[1]]))

print(sort_dicts)
Output:{2: 4, 1: 5, 6: 5, 7: 5, 3: 8, 4: 9, 5: 10}

按Value降序

dicts = {1:5, 2:4, 3:8, 4:9, 5:10, 6:5, 7:5}
sort_dicts = dict(sorted(dicts.items(), key = lambda x:[x[1]], reverse = True))

print(sort_dicts)

Output:{5: 10, 4: 9, 3: 8, 1: 5, 6: 5, 7: 5, 2: 4}
posted @   CharlesLC  阅读(374)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示