Last active
July 10, 2018 10:03
-
-
Save ayuLiao/2a04ef1cfe8b9e18b476622338a1c481 to your computer and use it in GitHub Desktop.
python中list的元素为dict,通过dict中具体某个元素,进行list排序
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 使用匿名函数进行排序,其中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