python sorted函数

from operator import itemgetter, attrgetter, methodcaller

student_tuples = [('john', 'A', 15),
('dave', 'A', 100),
('jane', 'B', 12),
('dave', 'B', 10),
]
messages = ['critical!!!', 'hurry!', 'standby', 'immediate!!']

print sorted(student_tuples, key=itemgetter(1,2)) # sorted by the index of tuple is one and then sorted by two
print sorted(student_tuples, key=lambda anonymous: anonymous[2])
print sorted(student_tuples, key=itemgetter(2))
print sorted(messages, key=methodcaller('count', '!')) # sorted by times of '!' descending

 


posted @ 2017-06-11 09:34  随便了888  阅读(405)  评论(0编辑  收藏  举报