如何使用python从嵌套的json文件中获取前10个项目?

以下是json文件数据:

 {
        "a": [
            {
                "incl": "34.86"
            },
            {
                "excl": "34.86"
            },
            {
                "m": "tb"
            },
            ...
        ]
        "b": [
            {
                "incl": "24.86"
            },
            {
                "excl": "14.86"
            },
            ...
        ]
      ...
    }

这是json文件的示例,

我想通过incl或excl获取前n个项(例如,“ a”,“ b”),该怎么做?

谢谢

评论
[妄]
[妄]

We use sorted() here, which returns a list of the keys in the desired order.
For top n keys by incl :

sorted(d , key = lambda x: d[x][0]['incl'],reverse = True)[:n] # n = 10

For top n keys by excl :

sorted(d , key = lambda x: d[x][0]['excl'],reverse = True)[:n]

注意:

  • Here d is the json
点赞
评论