列表包裹元组,指定元组中数字大小排序字段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)]
posted @ 2023-10-29 17:02  ty1539  阅读(6)  评论(0编辑  收藏  举报