Sum of several Python lists

Asked

Viewed 59 times

-2

I have a reception of several lists of different sizes and always the values is '0 and 1' as they are always changing can not write the code to add up. I cannot assign these lists to a variable, because I receive them like this. I receive this data may or so and need to add!

[0, 0, 0, 0, 0, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 1]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1]

sum = sum all numbers and give only one value

1 answer

0

The simplest way I can imagine now is for you to use the function sum for each list

resultado = sum([0, 0, 0, 0, 0, 1, 1, 1, 0]) + sum([0, 0, 0, 0, 0, 1, 1, 1, 0, 1])+sum([0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1])+sum([0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1])+sum([0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1]);

print(resultado);

outworking:

Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
25

Browser other questions tagged

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