python如何根据dict内值排序一个列表中的所有dict【面试题详解】

今天爱分享给大家带来python如何根据dict内值排序一个列表中的所有dict【面试题详解】,希望能够帮助到大家。
如何排序如下列表,根据name或age


[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

简单的做法;


newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])

高效的做法


from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name'))

人已赞赏
Python

python如何映射两个列表成为一个字典【面试题详解】

2020-12-28 15:57:01

Python

python如何根据值排序一个字典【面试题详解】

2020-12-28 15:58:49

'); })();