简体   繁体   English

按元组的第二个和第三个值对三元素元组列表进行排序和计数

[英]Sort & count list of 3-element tuples by second and third value of a tuple

I have such list of tuples我有这样的元组列表

l =[(1, 'project1', 'errorMessage1'), 
    (2, 'project1', 'errorMessage1'), 
    (3, 'project2', 'errorMessage1'),
    (1, 'project3', 'errorMessage2')]

I would like the first column to hold the sum of all values with distinct project and errorMessage, like this:我希望第一列包含具有不同项目和错误消息的所有值的总和,如下所示:

[(3, 'project1', 'errorMessage1'),
 (3, 'project2', 'errorMessage1'),
 (1, 'project3', 'errorMessage3')]

I tried Counter, and some other stuff but don't see how should I approach this.我尝试了 Counter 和其他一些东西,但不知道我应该如何解决这个问题。

You could solve this using a dictionary to hold the sum of the counts:您可以使用字典来保存计数总和来解决这个问题:

l =[(1, 'project1', 'errorMessage1'), 
    (2, 'project1', 'errorMessage1'), 
    (3, 'project2', 'errorMessage1'),
    (1, 'project3', 'errorMessage2')]

d = {}

for t in l:
    if t[1:] in d:
        d[t[1:]] += t[0]
    else:
        d[t[1:]] = t[0]

Output:输出:

>>> d
{('project1', 'errorMessage1'): 3,
 ('project2', 'errorMessage1'): 3,
 ('project3', 'errorMessage2'): 1}

Add a list comprehension to reformat the result:添加列表理解以重新格式化结果:

>>> [(v, *k) for k, v in d.items()]
[(3, 'project1', 'errorMessage1'),
 (3, 'project2', 'errorMessage1'),
 (1, 'project3', 'errorMessage2')]

Assuming you want to sum the 0th elements of the tuples, if you don't want to use a dict, you can also achieve this using itertools.groupby and sum like so:假设你想对元组的第 0 个元素求和,如果你不想使用字典,你也可以使用itertools.groupbysum来实现这一点,如下所示:

from itertools import groupby
from operator import itemgetter

input = [
    (1, 'project1', 'errorMessage1'),
    (2, 'project1', 'errorMessage1'),
    (3, 'project2', 'errorMessage1'),
    (1, 'project3', 'errorMessage2'),
]

def sum_by_project_and_error(input):
    # groupby needs the iterable to be sorted by the elements we want to group by.
    # We sort by project and error message (the 1st and 2nd element of the tuples) using itemgetter. 
    key_function = itemgetter(1, 2)
    sorted_input = sorted(input, key=key_function)
    grouped_input = groupby(sorted_input, key=key_function)

    for (project, error), group in grouped_input:
        yield sum(count for count, _, _ in group), project, error


output = list(sum_by_project_and_error(input))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按第二个元组元素对元组列表进行排序? - How to sort a list of tuples, by the second tuple element? Python元组列表中第二个和第三个元素的和,按第一个分组 - Python sum of second and third element of tuple in list of tuples grouped by first 通过添加元组的第二个和第三个元素对元组列表进行排序 - Sorting a list of tuples by the addition of second and third element of the tuple 如何根据元组中另一个值(第三个值)的降序对元组的排序列表(基于第二个值)中的相似值进行排序 - How to sort similar values in a sorted list (based on second value) of tuples based on another value(third value) in the tuple in descending order 将三元组的元列表转换为字典 - Convert 3-Element List of Tuples to Dictionary Python 3.x:如何按字符串值对具有相同第二个元素(计数)的元组列表进行排序? - Python 3.x: How do I sort a list of tuples that has the same second element (count) by string value? 返回 6 元素或 3 元素元组列表的函数 - Function that returns a list of 6-element or 3-element tuples 将元组的第二个元素(在元组列表中)获取为字符串 - Getting second element of a tuple (in a list of tuples) as a string 按Python 3中的特定元组元素对元组列表进行排序 - Sort list of tuples by specific tuple element in Python 3 根据元组列表中的第二个值找到元组后返回元组的第一个元素 - Returning first element of a tuple after finding the tuple according to the second value in a list of tuples
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM