Search code examples
pythonlistpython-3.xmappingdefaultdict

Mapping sums of defaultdict(list) to one list


I have a large collection of data formatted somewhat like the d.items() of a defaultdict(list). See below:

products = [(('blue'), ([2, 4, 2, 4, 2, 4, 2, 4, 2, 4], [2, 4, 2, 4, 2, 4, 2, 4, 2, 4], [2, 4, 2, 4, 2, 4, 2, 4, 2, 4])),
        (('yellow'), ([1, 3, 1, 3, 1, 3, 1, 3, 1, 3], [1, 3, 1, 3, 1, 3, 1, 3, 1, 3], [1, 3, 1, 3, 1, 3, 1, 3, 1, 3])),
        (('red'), ([1, 1, 1, 1, 1, 2, 5, 4, 6, 4], [2, 5, 3, 4, 8, 1, 1, 1, 1, 1], [8, 6, 3, 9, 2, 1, 1, 1, 1, 1]))]

And I want to map the sum of each data value in the nested lists to their corresponding counterparts in the same position or index to produce the final sums like follows:

['blue', 6, 12, 6, 12, 6, 12, 6, 12, '6.000000', 12]
['yellow', 3, 9, 3, 9, 3, 9, 3, 9, '3.000000', 9]
['red', 11, 12, 7, 14, 11, 4, 7, 6, '8.000000', 6]

With loops, it can be done easily as shown in this function:

def summation(products):

    sums = []

    for item in products:
            sums.append([(item[0]),
                     sum(int(x[0]) for x in item[1]),
                     sum(int(x[1]) for x in item[1]),
                     sum(int(x[2]) for x in item[1]),
                     sum(int(x[3]) for x in item[1]),
                     sum(int(x[4]) for x in item[1]),
                     sum(int(x[5]) for x in item[1]),
                     sum(int(x[6]) for x in item[1]),
                     sum(int(x[7]) for x in item[1]),
                     "{:.6f}".format(sum(float(x[8]) for x in item[1])),
                     sum(int(x[9]) for x in item[1])])

    for s in sums:
        print(s)

The problem arises when the size of products is millions, i.e. it is very time-consuming. So I thought of implementing mapping each value to its corresponding one in the nested list for the same key. This is what I tried:

def mappingSum(products):
    sums = []

    for item in products:
        sums.append([item[0], map((sum(x), sum(y), sum(z)) for x, y, z in item[1])])



    for s in sums:
        print(s)

However, I get the following error:

TypeError: map() must have at least two arguments.

I don't know how to resolve it and I am not sure whether map is the right tool to do my task.


Solution

  • From what I understand, you need to zip the sublists in the list and sum them up:

    >>> sums = [(key, [sum(value) for value in zip(*values)]) for key, values in products]
    >>> for s in sums:
    ...     print(s)
    ... 
    ('blue', [6, 12, 6, 12, 6, 12, 6, 12, 6, 12])
    ('yellow', [3, 9, 3, 9, 3, 9, 3, 9, 3, 9])
    ('red', [11, 12, 7, 14, 11, 4, 7, 6, 8, 6])