[英]Sorting a list of tuples by the addition of second and third element of the tuple
i have a list of tuples which is : 我有一个元组列表,它是:
[('g', 10, 2), ('o', 6, 11), ('v', 2, 4), ('t', 1, 15), ('x', 40, 3), ('m', 4, 4), ('k', 10, 2), ('f', 14, 1), ('p', 70, 90), ('l', 21, 7), ('n', 1, 27), ('a', 39, 70), ('d', 11, 10), ('h', 21, 10), ('c', 10, 19), ('b', 8, 1), ('e', 30, 39), ('i', 23, 29), ('r', 8, 7), ('q', 2, 2), ('s', 18, 86)]
and I'm struggling about how to sort them so they can be placed like this: 我正在努力解决如何对它们进行排序,以便它们可以像这样放置:
[('p', 70, 90), ('a', 39, 70), ('s', 18, 84), ('è', 27, 45), ('e', 30, 39), ('i', 23, 29), ('x', 40, 3), ('h', 21, 10), ('c', 10, 19), ('l', 20, 7), ('d', 11, 10), ('o', 6, 11), ('t', 1, 15), ('f', 14, 1), ('r', 8, 7), ('g', 10, 2), ('k', 10, 2), ('n', 1, 11), ('b', 8, 1), ('m', 4, 4), ('v', 2, 4), ('q', 2, 2)]
It should be the addition of the second and third element of the tuple , and when they are the same ex. 它应该是元组的第二个和第三个元素的添加,当它们是相同的ex时。 (a,10,9)
and (b,9,10)
they should be sorted alphabetically. (a,10,9)
和(b,9,10)
它们应按字母顺序排序。 It's written in python 3.5 and I can't call any library 它是用python 3.5编写的,我无法调用任何库
You can set a tuple as the key to sort your list: 您可以将元组设置为对列表进行排序的键:
sorted(lst, key = lambda x: (sum(x[1:]), x[0]))
In this way, it will firstly sort by the sum of the last two elements of the tuple and then by the first element of the tuple. 通过这种方式,它将首先按元组的最后两个元素的总和,然后按元组的第一个元素进行排序。
And if you like the result in descending order, as @Moses commented, you can specify the the reverse
parameter to be True
: 如果你喜欢降序的结果,正如@Moses评论的那样,你可以将reverse
参数指定为True
:
sorted(lst, key = lambda x: (sum(x[1:]), x[0]), reverse = True)
Update : To handle descending, ascending order differently, since the sum
here is numeric, you can negate the sum
as well. 更新 :要以不同的方式处理降序,升序,因为此处的sum
是数字,您也可以否定sum
。 In this way, it will be sorted in descending order for the sum
but alphabetically for the first letter. 通过这种方式,它会为降序排列sum
,但对于字母的第一个字母。
sorted(lst, key = lambda x: (-sum(x[1:]), x[0]))
sorted(a, key=lambda x: (sum(x[1:3]), x[0]))
Where a
is your list. 其中a
是你的清单。 If you need reversed: 如果你需要逆转:
sorted(a, key=lambda x: (sum(x[1:3]), x[0]), reverse=True)
a = [('g', 10, 2), ('o', 6, 11), ('v', 2, 4), ('t', 1, 15),
('x', 40, 3), ('m', 4, 4), ('k', 10, 2), ('f', 14, 1),
('p', 70, 90), ('l', 21, 7), ('n', 1, 27), ('a', 39, 70),
('d', 11, 10), ('h', 21, 10), ('c', 10, 19), ('b', 8, 1),
('e', 30, 39), ('i', 23, 29), ('r', 8, 7), ('q', 2, 2),
('s', 18, 86)]
Create a function that will return the sum of the items and use that function a the sort key. 创建一个函数,它将返回项目的总和,并使用该函数作为排序键。 I like to use operator.itemgetter() when extracting items from iterables. 我喜欢在从iterables中提取项目时使用operator.itemgetter()。
import operator
second_third_first_items = operator.itemgetter(1, 2, 0)
def key(thing):
*two_three, one = second_third_first_items(thing)
return (sum(two_three), one)
a.sort(key = key, reverse = True)
key
without operator.itemgetter 没有operator.itemgetter的key
def key(thing):
one, *two_three = thing[:3]
return (sum(two_three), one)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.