简体   繁体   English

通过添加元组的第二个和第三个元素对元组列表进行排序

[英]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.

相关问题 Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first 按元组的第二个和第三个值对三元素元组列表进行排序和计数 - Sort & count list of 3-element tuples by second and third value of a tuple 对元组列表进行排序,其中元组的第二个元素为列表 - Sorting a list of tuples with the second element of tuple being a list 将元组的第二个元素(在元组列表中)获取为字符串 - Getting second element of a tuple (in a list of tuples) as a string 如何按第二个元组元素对元组列表进行排序? - How to sort a list of tuples, by the second tuple element? 按每个元组的第一个元素对元组列表进行排序-Python 2.7 - Sorting list of tuples by first element of each tuple - Python 2.7 Python3:按元组第一个元素中包含的数据戳对元组列表进行排序 - Python3: Sorting a list of tuples by datastamp contained in first element of tuple 我想追加到新列表中,仅添加第三个元素与第一个元素的第三个元素相同的元组 - I want to append to a new list , only the tuples in which the third element is the same with the third element of the first tuple 如何首先相对于元组的第二个元素订购元组列表,然后相对于 python 中的元组的第三个元素订购元组列表? - How to order a list of tuple first with respect to second element of the tuple and then with respenct to third element of the tuple in python? 按第二个元素对元组列表进行排序,但如果多个元组的第二个元素匹配,则使用第一个元素进行排序 - sorting a list of tuple by second element but if the second element of mutiple tuple matches then sort using the first element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM