How can I write a loop to calculate each value of a list that is not predefined

Asked

Viewed 72 times

1

Hello, I’m beginner in programming and in this forum tb " I am having difficulty calculating the standard deviation of a list of values ( not a predefined list, it is done through an input) How can I create a loop to get the values stored in the list ?

I tried to use the for and it didn’t work, apparently he was taking the Dice instead of the values. What I want to know is: You can make a loop so that I can take a value from the list subtract it from a media, save the value and add it to the next value from the list ?

el = ((listaVal[0] - medialis) ** 2) + ((listaVal[1] - medialis) ** 2) + ((listaVal[2] - medialis) ** 2) + ( (listVal[3] - medialis) ** 2) + ((listaVal[4] - medialis) ** 2)

I was doing it this way, but it only works for a list with 5 elements no more and no less

  • What are the types of values you want to insert in this list? integers or real?

  • probably whole.

1 answer

0


To solve this question you can use the following algorithm:

import numpy as np

m = int(input('Digite o número de linhas: '))
n = int(input('Digite o número de colunas: '))

lista = list()
for i in range(1, m + 1):
    linha = list()
    for j in range(1, n + 1):
        while True:
            try:
                v = int(input(f'Digite o {j}º valor da {i}ª linha: '))
                break
            except ValueError:
                print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')
        linha.append(v)
    lista.append(linha)

d_p = np.std(lista)
print(f'\033[32mO desvio padrão é:\n{d_p:.2f}\033[m')

Note that even if you enter integer values, the result of the standard deviation will always be a floating point number.

See here the definition of standard deviation.

Note that in this algorithm I imported the numpy library, which I had already installed in my IDE.

When we run this algorithm we are asked the number of rows and columns.

Why is that?

Because with row number and columns I can assemble a structure of an array, more precisely a list of lists.

Also note that the nesting of blocks for assemble a list of lists containing all values that will be passed.

Subsequently the method std library numpy calculates the standard deviation of all values of all lists.

Now, if you just want to calculate the summing up of all standard deviations from the lists, you can use the following algorithm:

import numpy as np

n = int(input('Quantas lista desejas calcular? '))

soma = 0
for c in range(1, n + 1):
    lista = list(map(int, input(f'Digite todos os valores da {c}º lista: ').split()))
    d_p = np.std(lista)
    soma += d_p

print(f'\033[32mA soma dos desvios padrões são: {soma:.2f}\033[m')

Note that when we run this second algorithm we receive the following message:Quantas listas desejas calcular. At this time we must inform the number of lists. Then we received the following message: Digite todos os valores da 1ª lista. Right now we must type all the values in the same line, separated by a single space and then press enter.

Later we must enter the values of the other lists.

For each list the algorithm will calculate its desvio padrão and accumulate its values in the counting variable soma. then the value will be displayed.

Browser other questions tagged

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