python 字典列表排序operator.itemgetter()
举例:
import operator
x = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}] sorted_x = sorted(x, key=operator.itemgetter('name'))
print sorted_x
----------------------------- [{'age': 10, 'name': 'Bart'}, {'age': 39, 'name': 'Homer'}]
倒序输出:只需要加1个参数reverse=True即可:
sorted_x = sorted(x, key=operator.itemgetter('name'),reverse=True)
不断的总结,才能不断的提高;不断的思考,才能不断的进步!