编程语言
首页 > 编程语言> > python-排序

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

标签:__,name,22,python,age,sorted,排序,Bart
来源: https://www.cnblogs.com/jinmuqq222/p/14884273.html