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


原文地址:https://www.cnblogs.com/vickey-wu/p/6984316.html