python-排序

encoding: utf-8

列表中的字典按字段排序

if __name__ == "__main__":
	x = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}, {'name':'aart', 'age':22}]
        #  sorted_x = sorted(x, key=lambda x : x['name'], reverse=True)   
	sorted_x = sorted(x, key=lambda x : x['age'])  
	print(sorted_x)

# 返回
[{'age': 10, 'name': 'Bart'}, {'age': 22, 'name': 'aart'}, {'age': 39, 'name': 'Homer'}]
sandbox> exited with status 0
上一篇:2021-06-30


下一篇:关于python的sorted函数