python - 在python中对字典中的值进行排序
问题描述
如果输入为={"i":4,"m":1,"s":4,"p":2} 输出应为={"i":4,"s":4,"p": 2,"m":1} 我如何按降序对字典中的值进行排序
print(sorted(dict,reverse=True))。我用过这个但没有得到结果
解决方案
我现在无法测试这段代码,但它应该相当接近
# get sorted entries
s = sorted(d.items(), key=lambda e: e[1], reverse=True)
# since Python 3.6, dictionaries are sorted by entry
# if you're using an older Python, check out collections.OrderedDict
d = {k: v for (k,v) in s}
推荐阅读
- scala - 比较 DF 结构数组 spark 中的值
- browser - android 默认浏览器是否支持 service worker?
- python-3.x - Visual Studio 代码上的 Python AutoIndent
- virtualization - 使用 Proxmox API 获取 CPU 型号名称
- jquery - 如何禁用 jqueryUI menu() 小部件的悬停行为并添加一个单击事件?
- wix - 如何在设置验证期间从合并的“Microsoft_VC141_CRT_x64.msm”中提取 MSVC rutime 库
- html - 具有第二列动态宽度和省略号的固定宽度表
- excel - Excel VBA比较字符串以确定单元格的内容是否具有正确的格式(如果需要)
- r - 考虑 NA 列在 R 中创建漏斗转化率
- html-email - 如何根据电子邮件帐户调整我的电子邮件 html?