【发布时间】:2020-01-02 23:37:05
【问题描述】:
我正在使用 Python 3.7。我有一组 JSON 对象。我想根据每个 JOSN 对象中的一个值(“分数”)对对象数组进行排序。我无法弄清楚如何编写排序函数。我试过这个
>>> arr = [{'score': 10, 'name': 'Bob'}, {'score': 15, 'name':'Susan'}, {'score': 1, 'name': 'Skippy'}]
>>> arr
[{'score': 10, 'name': 'Bob'}, {'score': 15, 'name': 'Susan'}, {'score': 1, 'name': 'Skippy'}]
>>> arr.sort(key=json['score'], reverse=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'json' is not defined
>>> arr.sort(key=json['score'], reverse=True)
但我不知道如何从排序函数的“关键”部分引用 JSON 对象。
【问题讨论】:
标签: arrays json python-3.x sorting