【发布时间】:2019-09-09 21:41:36
【问题描述】:
我有一本像这样的字典
{
'Host-A': {'requests':
{'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
'total_hit_count': 9},
}
您可以看到'Host-A' 的值是一个字典,其中包含接收到的请求和每页上的点击数。问题是如何按降序对'requests' 进行排序。这样我就可以获得最高请求。
正确解决方案输出的示例如下:
{
'Host-A': {'requests':
{'GET /index.php/cronjob HTTP/1.0': {'code': '200', 'hit_count': 4},
'GET /index.php/dashboard HTTP/1.0': {'code': '200', 'hit_count': 3},
'GET /index.php/setup HTTP/1.0': {'code': '200', 'hit_count': 2}},
'total_hit_count': 9},
}
感谢您的帮助
【问题讨论】:
-
python 字典是键值对,不以任何方式排序。如果你愿意,你可以使用
OrderedDict:docs.python.org/3/library/collections.html#ordereddict-objects -
@Gassa 我已经修改了问题。
标签: python python-3.x sorting dictionary