-1
If I understand your question, you need to add each column.
Taking into account that voting is a tuple of tuples, you can do the following:
def assembleia(votacoes):
return tuple(map(sum, zip(*votacoes)))
What’s going on:
we are passing the tuples within polls to the function
zip
. We can do this by putting the*
before the name of the tuple:*votacoes
.The function
zip
will be responsible for transposing its voting matrix, that is, transforming what was a row matrix into a column matrix.We passed the function
sum
, that sums up all the elements of an iterable, and our column matrix for the functionmap
, who will be responsible for implementing thesum
in each column of our matrix.How map generates a type object
map
, we will pass your result to the functiontuple
, that 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)
assembelia(votacoes) returns a list of tuples?
– ppalacios
"Write an assembly function that given a tuple number of votes with the results of the vote in each constituency , returns a tuple with the total number of mandates assigned to each candidacy in the Assembly of the Republic. Assume that Voting has 22 entries, in which each entry is a Double with 15 values, i.e., the number of votes in each application." - I have already calculated the mandates allocated to each constituency. I need to add them all to get at national level. Returns a single
– João Machado
Try to make your question a little clearer, put an example of the result you expect.
– ppalacios
>>> assembly(votes) (0, 16, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 6, 0) that has to be the answer
– João Machado
from these votes I arrived at these mandates and by doing the report that showed me it returns the sum of the votes and not the mandates
– João Machado
Welcome to Sopt João. It would be nice if you edit your question and make it a little clearer, so the community can help you better.
– gato
If it is a sum of the columns, then my answer is right.
– ppalacios