How do I add entries from a Tuple and return a Tuple with an entry that has the sum of the previous Tuple entries? add by columns

Asked

Viewed 323 times

-1

as votações22 entradas

22 entries of a Tuple that must return in a Tuplo the sum of each value in the corresponding column

  • assembelia(votacoes) returns a list of tuples?

  • "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

  • Try to make your question a little clearer, put an example of the result you expect.

  • >>> assembly(votes) (0, 16, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 71, 6, 0) that has to be the answer

  • 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

  • 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.

  • If it is a sum of the columns, then my answer is right.

Show 2 more comments

1 answer

0

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:

  1. 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.

  2. The function zip will be responsible for transposing its voting matrix, that is, transforming what was a row matrix into a column matrix.

  3. We passed the function sum, that sums up all the elements of an iterable, and our column matrix for the function map, who will be responsible for implementing the sum in each column of our matrix.

  4. How map generates a type object map, we will pass your result to the function tuple, 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)
  • how I define paragraph?

  • sorry, I didn’t. What do you mean?

  • how I define q n is the column?

  • I’ll edit the question to make it clear

  • is not returning what is requested. returns the sum of votes and not mandates

  • Hmm, so I got the question wrong.

  • What represents a mandate?

  • represents an elected member

  • but these values will not appear in the shell. Only the sum of the mandates appears.

  • You definitely need to be clearer.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.