gpt4 book ai didi

Python3 使用 Lambda 和 Sort 对元组列表进行排序

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:43 24 4
gpt4 key购买 nike

<分区>

我有一个按第二个元素排序的元组列表,如下所示:

[('7', 10), ('2', 9), ('8', 9)]

我想像这样按第一个元素对它们进行排序:

[('2',9),('7', 10),('8', 9)]

我试过:

test_val = [('7', 10), ('2', 9), ('8', 9)]
test_val.sort(key=lambda x: x[0])

我从 here 中找到的

但是,列表未排序。

真实数据示例:

尝试之前:

[('7', 10), ('2', 9), ('8', 9), ('6', 8), ('24', 8), ('3', 7), ('5', 7), ('9', 6), ('35', 6), ('15', 5), ('16', 5), ('1', 4), ('14', 4), ('17', 3), ('19', 3), ('12', 2), ('39', 2), ('26', 1), ('25', 1), ('22', 0)]

尝试后:

[('1', 4), ('12', 2), ('14', 4), ('15', 5), ('16', 5), ('17', 3), ('19', 3), ('2', 9), ('22', 0), ('24', 8), ('25', 1), ('26', 1), ('3', 7), ('35', 6), ('39', 2), ('5', 7), ('6', 8), ('7', 10), ('8', 9), ('9', 6)]

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com