Python 字典 列表 复杂数据结构 排序

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}

sorted = sorted(x.iteritems(), key=lambda x: x[1], reverse=True)

import operator;sorted = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True)

 

x = [(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]

sorted = sorted(x.iteritems(), key=lambda x: x[1], reverse=True)

import operator;sorted = sorted(x.iteritems(), key=operator.itemgetter(1), reverse=True)

 

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

sorted = sorted(x, key=operator.itemgetter('name'))

 

 

参考文献:

https://blog.csdn.net/xiaoQL520/article/details/78134260

 

posted @ 2018-06-20 11:17  jhc888007  阅读(190)  评论(0编辑  收藏  举报