python列表中的元素按照自身某个索引的元素排序

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 获取列表的第二个元素
def takeSecond(elem):
    return elem[1]
 
# 列表
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
 
# 指定第二个元素排序
random.sort(key=takeSecond)
 
# 输出类别
print('排序列表:')
print(random)

Reference

posted @ 2023-10-11 20:12  pomolnc  阅读(3)  评论(0编辑  收藏  举报