How to add list values in tuples?

Asked

Viewed 4,053 times

5

I have for example a list with tuples, and inside these tuples I have 2 lists, where only the second list of each tuple varies: x= [(y, [1, 2]), (y, [3,4]), (y, [5, 6])]

How do I add the first values of the lists inside the tuples, like this:

1+3+5 = 9
2+4+6 = 12
Ficando: [9, 12]

Note: I just want to add up the values of the second lists of each List.

3 answers

4

In itertools there is a variant of zip which, by combining lists of different sizes, does not discard the elements further, but uses None for your values:

>>> teste = [[1,2,3],[4,5],[6,7,8,9]]
>>> list(izip_longest(*teste))
[(1, 4, 6), (2, 5, 7), (3, None, 8), (None, None, 9)]

Creditworthiness

Or, if specified a default value via fillvalue, this value is used (good to assign zero and not interfere with the sum):

>>> teste = [[1,2,3],[4,5],[6,7,8,9]]
>>> list(izip_longest(*teste, fillvalue=0))
[(1, 4, 6), (2, 5, 7), (3, 0, 8), (0, 0, 9)]

Thus, you can use some list understandings to get the list of sums in a single command:

>>> x= [(y, [1, 2, 3]), (y, [4, 5]), (y, [6, 7, 8, 9])]
>>> somas = [sum(numeros) for numeros in
...   izip_longest(*[segundo for primeiro,segundo in x], fillvalue=0)
... ]
>>> somas
[11, 14, 11, 9]

3


You can do something like:

x= [('y', [1, 2]), ('y', [3,4]), ('y', [5, 6])]
soma = [0]*2
for t in x:
    soma[0] += t[1][0]
    soma[1] += t[1][1]
print soma

This will return:

[9,12]

Edit

To fit the answer to your comment, one possibility is to do the following:

x = [('y', [1, 2]), ('y', [3,4]), ('y', [5, 6])]
soma = []
for t in x:
    for i in xrange(len(t[1])):
        if i == len(soma):
            soma.append(0)
        soma[i] += t[1][i]
print soma
  • yes that’s right, only there’s a problem. These lists of mine inside tuples can change size, and tuples can change size. that is, the lists can be of n elements and there can be n tuples

  • n tuples within that list or n tuples within tuples?

  • I have a list of tuples, and within these tuples I have other lists, which we can call listings. I within the lists can have n tuples and within the lists2 can have several values, that is, it can be like this:x= [('y', [1, 2, 3, 4]), ('y', [5, 6, 7, 8]), ('y', [9, 10, 11, 12]), (13,14,15,16)]

  • I did not understand this last tuple, but it would not be possible to standardize the tuple to always have a value and a list in it?

  • because I was wrong, I would be: x= [('y', [1, 2, 3, 4]), ('y', [5, 6, 7, 8]), ('y', [9, 10, 11, 12]), (y,[13,14,15,16])]

  • @Romy, see if my issue solves your problem. (:

Show 1 more comment

0

One-line solution, no use import nor for:

>>> sum([b[0] for a, b in x]), sum([b[1] for a,b in x])
(9, 12)

Version that returns a list:

>>> [sum([b[0] for a, b in x]), sum([b[1] for a,b in x])]
[9, 12]
  • This solution is not prepared for tuples of different sizes.

  • Correct Mauro, but this requirement was not explicit in the question asked.

Browser other questions tagged

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