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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迹忆客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值