小编Man*_*nix的帖子

Python 按值降序对嵌套字典进行排序

我有一本字典,看起来像这样

{
'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},
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,'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},
}
Run Code Online (Sandbox Code Playgroud)

我感谢您的帮助

python sorting dictionary python-3.x

5
推荐指数
1
解决办法
2049
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1

sorting ×1