python operator.itemgetter
import operator a = [("b",2),("a",1),("c",0)] b = sorted(a,key=operator.itemgetter(1)) # 使用元组的第二个元素进行排序 c = sorted(a,key=operator.itemgetter(1),reverse=True) print (a) print (b) print (c)
结果:
[('b', 2), ('a', 1), ('c', 0)] [('c', 0), ('a', 1), ('b', 2)] [('b', 2), ('a', 1), ('c', 0)]