分组Python元组列表

时间:2010-02-12 01:19:21

标签: grouping python

我有一个(标签,计数)元组列表,如下所示:

[('grape', 100), ('grape', 3), ('apple', 15), ('apple', 10), ('apple', 4), ('banana', 3)]

由此我想要使用相同的标签(相同的标签始终相邻)对所有值求和,并以相同的标签顺序返回一个列表:

[('grape', 103), ('apple', 29), ('banana', 3)]

我知道我可以通过以下方式解决它:

def group(l):
    result = []
    if l:
        this_label = l[0][0]
        this_count = 0
        for label, count in l:
            if label != this_label:
                result.append((this_label, this_count))
                this_label = label
                this_count = 0
            this_count += count
        result.append((this_label, this_count))
    return result

但是,有更多Pythonic /优雅/有效的方法吗?

7 个答案:

答案 0 :(得分:30)

itertools.groupby可以做你想做的事:

import itertools
import operator

L = [('grape', 100), ('grape', 3), ('apple', 15), ('apple', 10),
     ('apple', 4), ('banana', 3)]

def accumulate(l):
    it = itertools.groupby(l, operator.itemgetter(0))
    for key, subiter in it:
       yield key, sum(item[1] for item in subiter) 

>>> print list(accumulate(L))
[('grape', 103), ('apple', 29), ('banana', 3)]
>>> 

答案 1 :(得分:6)

使用itertools和list comprehensions

import itertools

[(key, sum(num for _, num in value))
    for key, value in itertools.groupby(l, lambda x: x[0])]
gnibbler指出

编辑:如果l尚未排序,则将其替换为sorted(l)

答案 2 :(得分:5)

import collections
d=collections.defaultdict(int)
a=[]
alist=[('grape', 100), ('banana', 3), ('apple', 10), ('apple', 4), ('grape', 3), ('apple', 15)]
for fruit,number in alist:
    if not fruit in a: a.append(fruit)
    d[fruit]+=number
for f in a:
    print (f,d[f])

输出

$ ./python.py
('grape', 103)
('banana', 3)
('apple', 29)

答案 3 :(得分:4)

>>> from itertools import groupby
>>> from operator import itemgetter
>>> L=[('grape', 100), ('grape', 3), ('apple', 15), ('apple', 10), ('apple', 4), ('banana', 3)]
>>> [(x,sum(map(itemgetter(1),y))) for x,y in groupby(L, itemgetter(0))]
[('grape', 103), ('apple', 29), ('banana', 3)]

答案 4 :(得分:2)

我的版本没有itertools
Read

答案 5 :(得分:0)

或者更简单易读的答案(没有itertools):

pairs = [('foo',1),('bar',2),('foo',2),('bar',3)]

def sum_pairs(pairs):
  sums = {}
  for pair in pairs:
    sums.setdefault(pair[0], 0)
    sums[pair[0]] += pair[1]
  return sums.items()

print sum_pairs(pairs)

答案 6 :(得分:0)

方法

def group_by(my_list):
    result = {}
    for k, v in my_list:
        result[k] = v if k not in result else result[k] + v
    return result 

用法

my_list = [
    ('grape', 100), ('grape', 3), ('apple', 15),
    ('apple', 10), ('apple', 4), ('banana', 3)
]

group_by(my_list) 

# Output: {'grape': 103, 'apple': 29, 'banana': 3}

您转换为list(group_by(my_list).items())等元组列表。