【发布时间】:2023-03-30 07:17:01
【问题描述】:
我正在尝试按高分对 JSON 进行排序,但这不起作用。 我的 JSON:
{"players": [{"test": [{"high_score": 1000}]}, {"test1": [{"high_score": 1200}]}, {"test2": [{"high_score": 3000}]}]}
我的 Python:
with open('score.json', "r") as json_file:
data = json.load(json_file)
json_file.close()
sorted_obj = data
sorted_obj['players'] = sorted(data['players'], key=lambda x: x['high_score'], reverse=True)
print(sorted_obj)
输出:
sorted_obj['players'] = sorted(data['players'], key=lambda x: x['high_score'], reverse=True)
KeyError: 'high_score''
我希望输出是:
{"players": [{"test2": [{"high_score": 3000}]}, {"test1": [{"high_score": 1200}]}, {"test": [{"high_score": 1000}]}]}
有谁知道如何解决这个问题?谢谢
【问题讨论】:
-
您的 JSON 结构毫无意义。请不要说必须用这个不能改。
-
如果我的回答有帮助,请标记为已接受。