iamapc
iamapc

Reputation: 69

Python - summing numbers that is the same

a = [2,2,2,3,4,5,5,6,6,6,7,7]
b = [1,2,3,4,5,6,7,8,9,10,11,12]

I want to sum the numbers in 'b' that in 'a' are the same and remove the duplicated numbers in 'a', so the output should look like:

a = [2,3,4,5,6,7]
b = [6,4,5,13,27,23]

Upvotes: 0

Views: 84

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180550

OrderedDict.fromkeys will create a set of the elements in a and keep order:

a = [2,2,2,3,4,5,5,6,6,6,7,7]
b = [1,2,3,4,5,6,7,8,9,10,11,12]

from collections import OrderedDict
from itertools import islice

od = OrderedDict.fromkeys(a,0,)

for ele in a:
    od[ele] += 1

it = iter(b)
sums = [sum(islice(it,v)) for v in od.values()]

print(list(od))
print(sums)
[2, 3, 4, 5, 6, 7]
[6, 4, 5, 13, 27, 23]

If you use a set you will have no guaranteed order, it is also unclear if you have elements that repeat later in your list a and what exactly happens if that is the case.

To work with later repeating elements:

a = [2, 2, 2, 3, 4, 5, 5, 6, 6, 7, 6, 7, 7]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,13]

from collections import OrderedDict
from itertools import islice, count

it = iter(a)
seq = 1
prev = next(it)
cn = count()
od = OrderedDict()
for ele in it:
    if ele == prev:
        seq += 1
    else:
        od[next(cn)] = seq
        seq = 1
    prev = ele
if prev == ele:
    seq += 1
    od[next(cn)] = seq


st_a = OrderedDict.fromkeys(a)

it = iter(b)

sums = [sum(islice(it, v)) for v in od.values()]
print(list(st_a))
print(sums)
[0, 1, 2, 3, 4, 5, 6, 7]
[6, 4, 5, 13, 17, 10, 11, 25]

Upvotes: 0

Malik Brahimi
Malik Brahimi

Reputation: 16721

Use a list comprehension, zipping the two lists together:

sums = [sum(y for x, y in zip(a, b) if x == i) for i in [j[0] for j in groupby(a)]]

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107357

You can use izip_longest and collections.defaultdict this is a fast and comprehensive way for solve this problem as it works if the length of a and b was not the same :

>>> from collections import defaultdict
>>> from itertools import izip_longest
>>> d=defaultdict(int)

>>> for i,j in izip_longest(a,b):
...   d[i]+=j
... 
>>> d
defaultdict(<type 'int'>, {2: 6, 3: 4, 4: 5, 5: 13, 6: 27, 7: 23})
>>> d.values()
[6, 4, 5, 13, 27, 23]

But as Padraic Cunningham noted dicts are not ordered although in this case the answer is true!!

as an alternative answer but less efficient you can use itertools.groupby :

>>> from itertools import izip_longest,groupby
>>> from operator import itemgetter
>>> [sum([i[1] for i in g]) for _,g in groupby(izip_longest(a,b),key=itemgetter(0))]
[6, 4, 5, 13, 27, 23]

Upvotes: 0

Related Questions