Python sum tuple list based on first tuple value

Suppose I have the following list tuples:

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

I want to summarize this list based on the same first tuple value:

 [(n,m),(n,k),(m,l),(m,z)] = m*k + l*z 

For myList

 sum = 2*5 + 3*6 = 28 

How can i get this?

+4
source share
4 answers

You can use collections.defaultdict :

 >>> from collections import defaultdict >>> from operator import mul >>> lis = [(0,2),(1,3),(2,4),(0,5),(1,6)] >>> dic = defaultdict(list) >>> for k,v in lis: dic[k].append(v) #use the first item of the tuple as key and append second one to it ... #now multiply only those lists which contain more than 1 item and finally sum them. >>> sum(reduce(mul,v) for k,v in dic.items() if len(v)>1) 28 
+4
source
 from operator import itemgetter from itertools import groupby def mul(args): # will work with more than 2 arguments, eg 2*3*4 return reduce(lambda acc, x: acc*x, args, 1) myList = [(0,2),(1,3),(2,4),(0,5),(1,6)] sorted_ = sorted(myList, key=itemgetter(0)) grouped = groupby(sorted_, key=itemgetter(0)) numbers = [[t[1] for t in items] for _, items in grouped] muls = [mul(items) for items in numbers if len(items) > 1] print sum(muls) 
0
source

This solution does it in one pass, unlike the more readable version of defaultdict , which takes two passes and can take up more space:

 myList = [(0,2),(1,3),(2,4),(0,5),(1,6)] sum_ = 0 once, twice = {}, {} for x, y in myList: if x in once: sum_ -= twice.get(x, 0) twice[x] = twice.get(x, once[x]) * y sum_ += twice[x] else: once[x] = y >>> sum_ 28 
0
source

Using the program below, even if you have several records, and not just two for one key, it will work

 #!/usr/local/bin/python3 myList = [(0,2),(1,3),(2,4),(0,5),(1,6),(1,2)] h = {} c = {} sum = 0 for k in myList: # if key value already present if k[0] in c: if k[0] in h: sum = sum - h[k[0]] h[k[0]] = h[k[0]] * k[1] else: h[k[0]] = c[k[0]] * k[1] sum = sum + h[k[0]] else: # stores key and value if first time though the loop c[k[0]] = k[1] print('sum is' + str(sum)) 
0
source

Source: https://habr.com/ru/post/1481707/


All Articles