汇总列表中的元组

时间:2018-09-09 23:01:31

标签: python python-3.x tuples

我有一个元组列表:

[(0, 1), (0, 1), (0, 0), (0, 0), (1, 0), (1, 0), (1, 1), (1, 0), (1, 0), (2, 0), (2, 1), (2, 0), (3, 0), (3, 1), (3, 1), (3, 0), (3, 0), (4, 0), (4, 1), (4, 0), (4, 1), (4, 1), (5, 0), (5, 0), (5, 1), (5, 1)]

我想求和元组的右边等于左边的元组,并将其放在另一个元组列表中,因此对于上面的列表,我将得到:

[(0,2),(1,1),(2,1),(3,2),(4,3),(5,2)]

我尝试过:

k=0
for i,TCtup in enumerate(wordsMatchingList):
    if wordsMatchingList[i][0]==k:
        TC_matches+=wordsMatchingList[i][1]
        print("k: {} /// TC_matches: {}".format(k,TC_matches)) #for checking
    else:
        groupedWordsMatchingList.append(tuple((k,TC_matches)))
        TC_matches=0
        k+=1

但是从k = 1开始,由于else条件,它每k循环少一遍。

谢谢

6 个答案:

答案 0 :(得分:3)

如果保证您的元组按这样的顺序排列-全部(0, x),然后所有(1, x),等等-您可以使用groupby

>>> xs = [(0, 1), (0, 1), (0, 0), (0, 0), (1, 0), (1, 0), (1, 1), (1, 0), (1, 0), (2, 0), (2, 1), (2, 0), (3, 0), (3, 1), (3, 1), (3, 0), (3, 0), (4, 0), (4, 1), (4, 0), (4, 1), (4, 1), (5, 0), (5, 0), (5, 1), (5, 1)]
>>> from itertools import groupby
>>> from operator import itemgetter
>>> groups = groupby(xs, key=itemgetter(0))
>>> ys = [(key, sum(map(itemgetter(1), group))) for key, group in groups]

如果它们不是,但是您可以对它们进行排序(您有一个列表,不仅是一个任意的可迭代对象,而且它不是很大,以至于对数线性时间会太昂贵) :

>>> groups = groupby(sorted(xs, key=itemgetter(0)), key=itemgetter(0))

如果无法对它们进行排序,则可以在进行过程中手动建立总计:

>>> from collections import Counter
>>> totals = Counter()
>>> for k, v in xs:
...     totals[k] += v
>>> ys = list(totals.items())

答案 1 :(得分:2)

另一种方法是使用collections中的defaultdict并迭代元组列表。

from collections import defaultdict

lst = [(0, 1), (0, 1), (0, 0), (0, 0), (1, 0), (1, 0), (1, 1), (1, 0), (1, 0), (2, 0), (2, 1), (2, 0), (3, 0), (3, 1), (3, 1), (3, 0), (3, 0), (4, 0), (4, 1), (4, 0), (4, 1), (4, 1), (5, 0), (5, 0), (5, 1), (5, 1)]

d = defaultdict(int)

for (u,v) in lst:
    d[u]+=v

# list(d.items()) [(0, 2), (1, 1), (2, 1), (3, 2), (4, 3), (5, 2)]

答案 2 :(得分:2)

另一种方式,

t.sort(key=lambda x: x[0]) #sort before groupby (required)
g=itertools.groupby(t, lambda x: x[0])
new_l = []
for k,v in g:
    new_l.append((k, sum([x[1] for x in v])))

答案 3 :(得分:0)

我建议使用带有groupby函数的库。例如,pandas可能有用

>>> s = pd.DataFrame(list_)
>>> s.groupby(0, as_index=False).sum().agg(tuple,1).tolist()

[(0, 2), (1, 1), (2, 1), (3, 2), (4, 3), (5, 2)]

答案 4 :(得分:0)

In [5]: [(j, sum([i[1] for i in a if i[0] == j])) for j in set([i[0] for i in a])]
Out[5]: [(0, 2), (1, 1), (2, 1), (3, 2), (4, 3), (5, 2)]

答案 5 :(得分:0)

lst = [(0, 1), (0, 1), (0, 0), (0, 0), (1, 0), (1, 0), (1, 1), (1, 0), (1, 0), (2, 0), (2, 1), (2, 0), (3, 0), (3, 1), (3, 1), (3, 0), (3, 0), (4, 0), (4, 1), (4, 0), (4, 1), (4, 1), (5, 0), (5, 0), (5, 1), (5, 1)]
[(i,sum([q[1] for q in lst if q[0] == i])) for i in range(lst[-1][0]+1)]

给予:

[(0,2),(1,1),(2,1),(3,2),(4,3),(5,2)]