Pythonで辞書のリストをソートする方法
Pythonで辞書のリストをソートする方法です。
list = []
item = {'intent_id':1, 'sentence':'バイバイ', 'score': 100}
list.append(item)
item = {'intent_id':2, 'sentence':'さよなら', 'score': 200}
list.append(item)
sort_list = sorted(list, key=lambda x:x['score'], reverse=True)
for item in sort_list:
print(item['sentence'] + '[' + str(item['score']) + ']')これでソートできます。
リスト内の辞書のscoreの降順にソートしています。
普通のソートにしたい場合は「reverse=True」を消してください。
検索しても、なかなか見つけられなったので書きました。


