一只斗牛犬
>>> from operator import itemgetter>>> data = [('abc', 121),('abc', 231),('abc', 148), ('abc',221)]>>> sorted(data,key=itemgetter(1))[('abc', 121), ('abc', 148), ('abc', 221), ('abc', 231)]IMO使用itemgetter在这种情况下,比@cheeken的解决方案更具可读性。它也更快,因为几乎所有的计算都将在c侧(无双关意),而不是通过使用lambda.>python -m timeit -s "from operator import itemgetter; data = [('abc', 121),('abc', 231),('abc', 148), ('abc',221)]" "sorted(data,key=itemgetter(1))"1000000 loops, best of 3: 1.22 usec per loop>python -m timeit -s "data = [('abc', 121),('abc', 231),('abc', 148), ('abc',221)]" "sorted(data,key=lambda x: x[1])"1000000 loops, best of 3: 1.4 usec per loop