Ere*_*ğlu 2 python sorting dictionary list
我有一个对象列表如下所示:
[{'id': 17L,
'price': 0,
'parent_count': 2},
{'id': 39L,
'price': 0,
'parent_count': 1},
{'id': 26L,
'price': 2.0,
'parent_count': 4},
{'id': 25L,
'price': 2.0,
'parent_count': 3}]
Run Code Online (Sandbox Code Playgroud)
我想对对象进行'parent_count'排序,看起来像这样:
[{'id': 39L,
'price': 0,
'parent_count': 1},
{'id': 17L,
'price': 0,
'parent_count': 2},
{'id': 25L,
'price': 2.0,
'parent_count': 3},
{'id': 26L,
'price': 2.0,
'parent_count': 4}]
Run Code Online (Sandbox Code Playgroud)
有谁知道一个功能?
Sve*_*ach 12
使用operator.itemgetter("parent_count")的key参数list.sort():
from operator import itemgetter
my_list.sort(key=itemgetter("parent_count"))
Run Code Online (Sandbox Code Playgroud)