python - 如何根据字典的值对字典进行降序排序?
问题描述
orders = {
'apple: 54,
'banana': 56,
'orange': 72,
'peach': 48,
'grape': 41
}
给定这种格式的字典,如何按值降序对字典进行排序?
解决方案
您可以使用 sorted 函数来执行此操作。
orders = {
'apple': 54,
'banana': 56,
'orange': 72,
'peach': 48,
'grape': 41
}
d = sorted(orders.items(), key=lambda x:x[1], reverse=True)
print(dict(d))
输出:
{‘橙’:72,‘香蕉’:56,‘苹果’:54,‘桃’:48,‘葡萄’:41}
推荐阅读
- amazon-web-services - Lambda SDK 调用无响应
- node.js - writing nodejs applications in kotlin with intellij idea ce
- xml - 检索上周在 Taleo 中接受的报价
- c++ - 将 "enum: int64_t" 值写入 std::ostringstream 会将其截断为 int
- azure - 将 Linux 服务器连接到 Azure Log Analytics 工作区的代理的名称是什么?
- java - 在 JavaFX 中使用动画计时器时如何解决帧率下降问题?
- python - 使用 struct.pack 和 struct.unpack 的问题
- emacs - org-mode 表头格式未正确对齐
- c# - 具有可定义列的 WPF Datagrid UserControl
- netsuite - NetSuite CASE WHEN 不工作,即使所有单独的语句都有效