Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of lists of tuples, group by first element and add second elements

Let's say I have the following list of lists of tuples:

tuples = [
             [ 
                 ('2017-04-11', '2000000.00'), 
                 ('2017-04-12', '1000000.00'), 
                 ('2017-04-13', '3000000.00')
             ],
             [
                 ('2017-04-12', '472943.00'), 
                 ('2017-04-13', '1000000.00')
             ]
             # ...
         ]

How would I go about grouping them based off of the first element (date) and adding the other element.

For instance I'd like something like this:

tuples = [('2017-04-11', '2000000.00'), ('2017-04-12', '1472943.00'), ('2017-04-13', '4000000.00')],
like image 913
Borko Kovacev Avatar asked Dec 24 '22 18:12

Borko Kovacev


2 Answers

The solution using itertools.chain.from_iterable, itertools.groupby and sum functions:

import itertools, operator

tuples = [
         [('2017-04-11', '2000000.00'), ('2017-04-12', '1000000.00'), ('2017-04-13', '3000000.00')],
         [('2017-04-12', '472943.00'), ('2017-04-13', '1000000.00')]
         ]

result = [(k, "%.2f" % sum(float(t[1]) for t in g)) 
          for k,g in itertools.groupby(sorted(itertools.chain.from_iterable(tuples)), operator.itemgetter(0))]

print(result)

The output:

[('2017-04-11', '2000000.00'), ('2017-04-12', '1472943.00'), ('2017-04-13', '4000000.00')]
like image 81
RomanPerekhrest Avatar answered Dec 30 '22 14:12

RomanPerekhrest


First, flat a list of tuples out of a list of lists of tuples, and then use itertools.groupby,

import itertools 
import operator

lists = [
         [('2017-04-11', '2000000.00'), ('2017-04-12', '1000000.00'), ('2017-04-13', '3000000.00')],
         [('2017-04-12', '472943.00'), ('2017-04-13', '1000000.00')]
         ]

# Step 1: Flat a list of tuples out of a list of lists of tuples
list_tuples = [t for sublist in lists for t in sublist]
'''
[('2017-04-11', '2000000.00'), ('2017-04-12', '1000000.00'), ('2017-04-13', '3000000.00'), ('2017-04-12', '472943.00'), ('2017-04-13', '1000000.00')]
'''

# Step 2: Groupby
results = list()

for key, group in itertools.groupby(sorted(list_tuples), operator.itemgetter(0)):
    s = sum(float(t[1]) for t in group)
    results.append((key, s))

print(results)
#[('2017-04-11', 2000000.0), ('2017-04-12', 1472943.0), ('2017-04-13', 4000000.0)]
like image 32
SparkAndShine Avatar answered Dec 30 '22 14:12

SparkAndShine