Sum of elements in lists

-1
[[3, 2, 7], [8, -2, 5], [-1, 4, 3], [2, 2, -9]]

Next, it saves in three different variables, ignoring the negative values, the sum of the elements separately. That is, the sum of the first element of each sublist, the sum of the second element of each sublist, and the sum of the third element of each sublist. Then create a dictionary with key / value matching from numbers 1 through 15, for example:

{1:"Um", 2:"Dois" … 15:"Quinze"}.

To finish, matching the result value to the dictionary key, displays the result individually in text form.

I started with this code. My first question is do for if else cycle? How do I get it to add only the positives? What do I call the blocks? in the same way as in the lists 0,1,2,3?

lista=[[3,2,7], [8,-2,5], [-1,4,3], [2,2,-9]]

var1=""
var2=""
var3=""

for int in range(len(lista))
    if (int in lista)
    var1= 
    
asked by anonymous 11.04.2017 / 12:49

2 answers

1

Consider the input list:

values = [[3, 2, 7], [8, -2, 5], [-1, 4, 3], [2, 2, -9]]

In order to sum the values of each list in a certain index, we must then redefine the list according to our need. To do this, we use the native function zip :

values = list(zip(*values))
  

Note: Using * causes each position in the list to be passed as a parameter to the function. Making zip(*values) is the equivalent of zip(values[0], values[1], values[2]) , but with less code and much easier to read.

In this way, our list will be as follows:

[(3, 8, -1, 2), (2, -2, 4, 2), (7, 5, 3, -9)]

That is, we created a tuple with the values in index 0 of each list, another with the values of index 1 and another with the values in index 2. We now only add the values when they are not negative, storing the result in variables, using the native function sum .

sum_1 = sum(i for i in values[0] if i >= 0) # Resulta: 13
sum_2 = sum(i for i in values[1] if i >= 0) # Resulta: 8
sum_3 = sum(i for i in values[2] if i >= 0) # Resulta: 15

You are also asked to define a dictionary with the numbers in full. So:

texts = {
  1: "um",
  2: "dois",
  3: "três",
  4: "quatro",
  5: "cinco",
  6: "seis",
  7: "sete",
  8: "oito",
  9: "nove",
  10: "dez",
  11: "onze",
  12: "doze",
  13: "treze",
  14: "catorze",
  15: "quinze",
}

Finally, the result is displayed:

print("A soma 1 resultou em: {}".format(texts[sum_1]))
print("A soma 2 resultou em: {}".format(texts[sum_2]))
print("A soma 3 resultou em: {}".format(texts[sum_3]))

What will be:

A soma 1 resultou em: treze
A soma 2 resultou em: oito
A soma 3 resultou em: quinze

See the code working in Repl.it or Ideone .

    
11.04.2017 / 13:49
0

You need to look at the problem as an array (which is accessed: matriz[posicao_i][posicao_j] ))

1. Leave the column fixed and go through all the rows, then doing its conditions and operations; 2. increase column position; 3. repeat.

# para somar ignorando os valores negativos:

lista = [[3,2,7], [8,-2,5], [-1,4,3], [2,2,-9]]

valores = []

for coluna in range(len(lista[0])):
    soma = 0

    for linha in range(len(lista)):
        if(lista[linha][coluna] >= 0):
            soma += lista[linha][coluna]

    valores.append(soma)

print(valores)
    
11.04.2017 / 13:27