在 Python 中对元组列表进行排序
使用 sorted()
函数的 key 参数对元组列表进行排序,例如 sorted_list = sorted(list_of_tuples, key=lambda t: t[1])
。 该函数将按指定索引处的元组元素对元组列表进行排序。
list_of_tuples = [(1, 100), (2, 50), (3, 75)]
# ✅ 按索引处的元素对元组列表进行排序(升序)
sorted_list = sorted(list_of_tuples, key=lambda t: t[1])
print(sorted_list) # 👉️ [(2, 50), (3, 75), (1, 100)]
# ----------------------------------------------------
# ✅ 按索引处的元素以降序对元组列表进行排序
sorted_list_descending = sorted(
list_of_tuples,
key=lambda t: t[1],
reverse=True
)
# 👇️ [(1, 100), (3, 75), (2, 50)]
print(sorted_list_descending)
sorted
函数接受一个迭代并从迭代中的项目返回一个新的排序列表。
该函数采用一个可选的 key 参数,可用于按不同的标准进行排序。
list_of_tuples = [(1, 100), (2, 50), (3, 75)]
sorted_list = sorted(list_of_tuples, key=lambda t: t[1])
print(sorted_list) # 👉️ [(2, 50), (3, 75), (1, 100)]
可以将 key 参数设置为确定排序标准的函数。
该示例按每个元组中的第二项(索引 1)对元组列表进行排序。
如果我们需要按降序(从大到小)对元组列表进行排序,请在调用 sorted()
函数时将 reverse 参数设置为 true。
list_of_tuples = [(1, 100), (2, 50), (3, 75)]
sorted_list_descending = sorted(
list_of_tuples,
key=lambda t: t[1],
reverse=True
)
# 👇️ [(1, 100), (3, 75), (2, 50)]
print(sorted_list_descending)
我们还可以使用这种方法按多个索引对元组列表进行排序。
list_of_tuples = [(1, 3, 100), (2, 3, 50), (3, 2, 75)]
# ✅ sort list of tuples by second and third elements
sorted_list = sorted(
list_of_tuples,
key=lambda t: (t[1], t[2])
)
print(sorted_list) # 👉️ [(3, 2, 75), (2, 3, 50), (1, 3, 100)]
该示例按每个元组中索引 1 和 2 处的元素对元组列表进行排序。
由于第三个元组中的第二项是最低的,所以它被移到前面。
第一个和第二个元组中的第二个项目相等(都是 3),所以比较第三个项目,因为 50 小于 100,所以它排在第二位。
使用 lambda
函数的替代方法是使用 operator.itemgetter()
方法来指定我们想要排序的索引。
from operator import itemgetter
list_of_tuples = [(1, 100), (2, 50), (3, 75)]
sorted_list = sorted(list_of_tuples, key=itemgetter(1))
print(sorted_list) # 👉️ [(2, 50), (3, 75), (1, 100)]
operator.itemgetter
方法返回一个可调用对象,该对象在指定索引处获取项目。
例如,x = itemgetter(1)
,然后调用 x(my_tuple)
,返回 my_tuple[1]
。
我们还可以使用此方法按多个索引进行排序。
from operator import itemgetter
list_of_tuples = [(1, 3, 100), (2, 3, 50), (3, 2, 75)]
# ✅ 按第二个和第三个元素对元组列表进行排序
sorted_list = sorted(list_of_tuples, key=itemgetter(1, 2))
print(sorted_list) # 👉️ [(3, 2, 75), (2, 3, 50), (1, 3, 100)]
使用 itemgetter
方法比使用 lambda
函数更快,但它也更加隐含。
或者,我们可以使用 list.sort()
方法。
使用 list.sort() 方法的 key 参数对元组列表进行适当的排序,例如 list_of_tuples.sort(key=lambda t: t[1])。 list.sort() 方法将按指定索引处的元组元素对元组列表进行排序。
list_of_tuples = [(1, 100), (2, 50), (3, 75)]
list_of_tuples.sort(key=lambda t: t[1])
print(list_of_tuples) # 👉️ [(2, 50), (3, 75), (1, 100)]
list.sort
方法对列表进行就地排序,它仅使用 <
项目之间的比较。
该方法采用以下 2 个仅关键字参数:
名称 | 描述 |
---|---|
key | 一个接受 1 个参数的函数,用于从每个列表元素中提取比较键 |
reverse | 一个布尔值,表示是否应反转每个比较 |
sort
方法采用的另一个关键字参数是 reverse 。
list_of_tuples = [(1, 100), (2, 50), (3, 75)]
list_of_tuples.sort(key=lambda t: t[1], reverse=True)
print(list_of_tuples) # 👉️ [(1, 100), (3, 75), (2, 50)]
注意
,list.sort 方法会在原地改变列表并返回 None。
如果我们需要一个新的列表实例而不是就地突变,我们可以使用 sorted()
函数。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。