Summing value by value in an array with Python

Asked

Viewed 3,686 times

1

I need to sum up all the possible values within one array to find out where an error is in a database.

Code until then:

valores = [50.70,1.80,121.37,100.00,3887.42,248.17,5650.31,702.00,556.90,54.77,2612.55,2414.05,1077.80,2612.55,47.90,535.56,1157.83,3116.31,250.00,780.00,1799.26,1732.09,2468.94,2612.55,715.50,550.00,600.00,890.52,1714.31,1714.61,150.35,2499.70,477.00,90.00,100.00,2412.12,3348.36,2612.55,47.00]
getValor = 0
soma = []
while getValor <= len(valores):
    soma[getValor] = valores[getValor] + valores[0]
    getValor = getValor + 1 
print(soma[getValor])

I would repeat that loop for each value within the array, changing the valores[0] for valores[1] and so on. This code returns the error:

Indexerror: list index out of range

How do I do it? How wrong is my logic?

  • soma was declared as an empty list, so do lista[0] already returns this error. Maybe what you are looking for is the function append(), to add a value at the bottom of the list. Anyway, try using for in place of while, implementation is much simpler.

4 answers

5

Python is a language that allows us to do things more easily. Given its array, a simple and lean way of safazer the sum using a low amount of variables and lines would be like this:

for valor in valores:
    somas = [valor + val for val in valores]

2

A way pythonica to do this is using a Python feature called List Comprehension:

valores = [50.70,1.80,121.37,100.00,3887.42,248.17,5650.31,702.00,556.90,54.77,2612.55,2414.05,1077.80,2612.55,47.90,535.56,1157.83,3116.31,250.00,780.00,1799.26,1732.09,2468.94,2612.55,715.50,550.00,600.00,890.52,1714.31,1714.61,150.35,2499.70,477.00,90.00,100.00,2412.12,3348.36,2612.55,47.00]
soma = [(val_1 + val_2) for val_1 in valores for val_2 in valores]
for val in soma:
    print(val)

and if you do not want to add the repeated elements can be done easily added the if:

soma = [(val_1 + val_2) for val_1 in valores for val_2 in valores if val_1 != val_2]

See about List comprehension

1


If I understood correctly what you want would be something like this:

valores = [50.70,1.80,121.37,100.00,3887.42,248.17,5650.31,702.00,556.90,54.77,2612.55,2414.05,1077.80,2612.55,47.90,535.56,1157.83,3116.31,250.00,780.00,1799.26,1732.09,2468.94,2612.55,715.50,550.00,600.00,890.52,1714.31,1714.61,150.35,2499.70,477.00,90.00,100.00,2412.12,3348.36,2612.55,47.00]
somas = []
for valor1 in valores:
    for valor2 in valores:
        somas.append(valor1 + valor2)
for valor in somas:
    print(valor)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I did this with logic that seems to indicate that is adding all possible combinations (I did not eliminate the duplications), and I fixed the problem that was not growing the list of sums with the method append(), and did it the way pythonica that is not using while and yes taking all values directly with for. In fact I don’t even know if you need the list of sums, you only need to do other things with them, otherwise you just print directly, many people create variables without need because they do not know the true vocation of variable.

  • It worked perfectly, I’m studying your code sent to get this logical knowledge. Thank you very much!

1

You can use the function sum() python passing parameter the array you want to receive the sum.

So if you use sum(valores) he returns the result.

Browser other questions tagged

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