列表包裹元组,指定元组中数字大小排序字段operator用法
import operator
somelist = [(1,5,8),(6,2,4),(9,7,5)]
somelist.sort(key=operator.itemgetter(0))
print(somelist)
# [(1, 5, 8), (6, 2, 4), (9, 7, 5)]
somelist.sort(key=operator.itemgetter(1))
print(somelist)
# [(6, 2, 4), (1, 5, 8), (9, 7, 5)]
somelist.sort(key=operator.itemgetter(2))
print(somelist)
# [(6, 2, 4), (9, 7, 5), (1, 5, 8)]
写入自己的博客中才能记得长久