根据列表内字典的某个KEY 进行排序

方法1 (lambda):

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 
备注:  如果key类型为不同类型,注意一下转换,比如int  (k: int(k['name']))

方法2:

from operator import itemgetter

newlist = sorted(list_to_be_sorted, key=itemgetter('name')) 

newlist = sorted(list_to_be_sorted, key=itemgetter('name'), reverse=True)


This entry was posted on 2018 年 03 月 15 日 and is filed under IT DOC. Written by: . You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.