Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

python - Sum numbers by letter in list of tuples

I have a list of tuples:

[ ('A',100), ('B',50), ('A',50), ('B',20), ('C',10) ]

I am trying to sum up all numbers that have the same letter. I.e. I want to output

[('A', 150), ('B', 70), ('C',10)] 

I have tried using set to get the unique values but then when I try and compare the first elements to the set I get

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Any quick solutions to match the numbers by letter?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a one(and a half?)-liner: group by letter (for which you need to sort before), then take the sum of the second entries of your tuples.

from itertools import groupby
from operator import itemgetter

data = [('A', 100), ('B', 50), ('A', 50), ('B', 20), ('C', 10)]
res = [(k, sum(map(itemgetter(1), g)))
       for k, g in groupby(sorted(data, key=itemgetter(0)), key=itemgetter(0))]
print(res)
// => [('A', 150), ('B', 70), ('C', 10)]

The above is O(n log n) — sorting is the most expensive operation. If your input list is truly large, you might be better served by the following O(n) approach:

from collections import defaultdict

data = [('A', 100), ('B', 50), ('A', 50), ('B', 20), ('C', 10)]

d = defaultdict(int)
for letter, value in data:
    d[letter] += value
res = list(d.items())
print(res)
// => [('B', 70), ('C', 10), ('A', 150)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...