现有list如
list_to_be_sorted = [{'lv': 44, 'age': 10},
{'lv': 12, 'age': 39},
{'lv': 88, 'age': 22},
{'lv': 6, 'age': 2},
{'lv': 3, 'age': 4}, ]
将该列表按照age字段的value进行排序。
from operator import itemgetter list_to_be_sorted = [{'lv': 44, 'age': 10}, {'lv': 12, 'age': 39}, {'lv': 88, 'age': 22}, {'lv': 6, 'age': 2}, {'lv': 3, 'age': 4}, ] new_list = sorted(list_to_be_sorted, key=itemgetter('age')) print(new_list)
如果需要按照lv与age字段value的乘积作为排序标准,可以参考以下方式:
list_to_be_sorted = [{'lv': 44, 'age': 10}, {'lv': 12, 'age': 39}, {'lv': 88, 'age': 22}, {'lv': 6, 'age': 2}, {'lv': 3, 'age': 4}, ] new_list = sorted(list_to_be_sorted, key=lambda k: k['lv'] * k['age']) print(new_list)