在嵌套字典中查找最大值

Ale*_*exZ 11 python dictionary

d = {
    "local": {
        "count": 1,
        "health-beauty": {
            "count": 1,
            "tanning": {"count": 1}
        }
    },
    "nationwide": {"count": 9.0},
    "travel": {"count": 0}
}    
Run Code Online (Sandbox Code Playgroud)

在这个例子中"nationwide"是最大的.

代码如下,以便更容易附加到脚本:

d = {'travel': {'count': 0}, 'local': {'count': 1, 'health-beauty': {'count': 1, 'tanning': {'count': 1}}}, 'nationwide': {'count': 9.0}}
Run Code Online (Sandbox Code Playgroud)

JBe*_*rdo 12

>>> max(d, key=lambda x: d[x]['count'])
'nationwide'
Run Code Online (Sandbox Code Playgroud)

  • 这不会工作..说如果你的内部字典是最大的(因为它甚至不考虑嵌套字典) (2认同)