迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Python >

在 Python 中对元组列表进行排序

作者:迹忆客 最近更新:2023/04/22 浏览次数:

使用 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() 函数。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Python 中的 Pandas 插入方法

发布时间:2024/04/23 浏览次数:112 分类:Python

本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。

Pandas 重命名多个列

发布时间:2024/04/22 浏览次数:199 分类:Python

本教程演示了如何使用 Pandas 重命名数据框中的多个列。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便