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

发布时间 2023-10-11 20:17:28作者: pomolnc
title:
aliases:
- python列表按元素排序
tags:
- Python/数据处理 

category:
stars:
url:

creation-time: 2023-07-31 15:26
modification-time: 
#!/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