Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Last active July 10, 2018 10:03
Show Gist options
  • Save ayuLiao/2a04ef1cfe8b9e18b476622338a1c481 to your computer and use it in GitHub Desktop.
Save ayuLiao/2a04ef1cfe8b9e18b476622338a1c481 to your computer and use it in GitHub Desktop.
python中list的元素为dict,通过dict中具体某个元素,进行list排序
# 使用匿名函数进行排序,其中k就是list中的元素,也就是一个dict
newlist = sorted(list_to_be_sorted, key=lambda k: k['name'])
#使用itemgetter
from operator import itemgetter
# reverse=True表示降序
newlist = sorted(list_to_be_sorted, key=itemgetter('name'),reverse=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment