我的 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/