我有使用json转储创建的这个Json对象:
data.append({"word":word,'list':list , "count":count})
value=json.dumps({"data":data })
{"data": [{"count": 1, "word": "bob", "list": [1]}, {"count": 10, "word": "lola", "list": [2,7]}]}
我想根据" count"订购此对象,而是输出:
{"data": [{"count": 1O, "word": "lola", "list": [2,7]}, {"count": 1, "word": "bob", "list": [2]}]}
有关如何订购此类对象的任何帮助?我已经尝试过这个解决方案:来自Martijn Pieters的How to get sorted list inside a dictionary with json.dumps(),但是根据' list'不算数。
提前谢谢
答案 0 :(得分:5)
替换
行value=json.dumps({"data":data })
与
value = json.dumps({'data': sorted(data, key=lambda x: x['count'], reverse=True)})
你应该
{"data": [{"count": 10, "list": [2, 7], "word": "lola"}, {"count": 1, "list": [1], "word": "bob"}]}