how do I add entries from a tuple and return a tuple with an entry that has the sum of the previous d to tuple entries? add by columns

-1

22 entries of a tuple that will have to return in a tuple the sum of each value in the corresponding column

    
asked by anonymous 19.10.2015 / 23:23

1 answer

0

If I understood your question, you need to add each column.

Given that votacoes is a tuple of tuples, you can do the following:

def assembleia(votacoes):
    return tuple(map(sum, zip(*votacoes)))

What's happening:

  • We are passing tuples into polls for the zip function. We can do this by putting * before the tuple name: *votacoes .

  • We pass the zip function, which sums all the elements of an iterable, and our column array for the sum function, which will be responsible for applying the map function in each column of our matrix.

  • As the map generates an object of type sum , we will pass its result to the function map , which will generate the tuple you need.

    According to your example:

    In [1]: votacoes = (
    ....:     (0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0,),
    ....:     (0, 1, 1,  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 1, 11,  0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0,),
    ....:     (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,),
    ....:     (0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,),
    ....:     (0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,),
    ....:     (0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,),
    ....:     (0, 5, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 3, 0,),
    ....:     (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 2, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 2, 0,),
    ....:     (0, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,),
    ....:     (0, 4, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 0,),
    ....:     (0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,),
    ....:     (0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,),
    ....:     (0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,),
    ....:     (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,),
    ....: )
    In [2]: tuple(map(sum, zip(*votacoes)))
    Out[2]: (0, 16, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 6, 0)
    
        
  • 20.10.2015 / 00:02