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}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)