python - 如何获取json文件中特定较低级别 key 的最高值的顶级 key ?

标签 python

我的 json 文件如下所示:

{
    "One": {
        "action": "One",
        "delay": 1558834290.2415142,
        "seconds": 60,
        "score": 10,
        "delta": 1558828290.2415142,
        "grace_sec": 6060
    },
    "Two": {
        "action": "Two",
        "delay": 1558834292.6928985,
        "seconds": 60,
        "score": 9,
        "delta": 1558828292.6928985,
        "grace_sec": 6060
    }
}

我想要获取具有最高 score 值的顶级 key ,因此在本例中我需要 key One

我已经尝试过:

with open("streak.json", "r") as read_file:
                data = json.load(read_file)

highest = max(int(d['score'] for d in data.values()))

但我得到 TypeError: int() argument must be a string, a bytes-like object or a number, not 'generator'

最佳答案

生成(inner_value,outer_key)元组以获取所需的值。

max((int(value['score']), key) for key, value in data.items())

这应该评估这个元组

(10, 'One') 

您可以根据需要进行处理。

关于python - 如何获取json文件中特定较低级别 key 的最高值的顶级 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56314569/

相关文章:

python - python 解释器中的 more-ing 或 less-ing 输出

python - 使用 redis.StrictRedis() 在 redis 中插入一个新数据库

Python 单元测试 : make nose show failed assertions values

python - Pandas 中的排序函数,返回困惑的数据

python - python计算不同数据帧中两个列表之间的匹配

python - 数组 Python 的复杂子集和合并

Python、Windows服务导入报错

python - 导入 Turicreate 出现错误 No module named 'turicreate.cython.cy_unity'

python - 使用 plotly 修改轴范围

python - 在非矩形二维网格上高效地找到最近点的索引