How to store values of a list in individual variables without knowing its size?

Asked

Viewed 932 times

0

I need to assign each integer value within a list of at least 1 item and at most 10 items to a separate variable. Then decrease 1 of each item, add them and print the result. These numerical values will be obtained by a input. For example:

#Se ela tiver 2 itens:

lista = input().split()

#convertendo para tipo inteiro:

def int_converter(list_x):
    counter = 0
    while 1 == 1:
        list_x[counter] = int(list_x[counter])
        if counter == (len(list_x) - 1):
            return list_x
        else:
            counter += 1
            continue

int_converter(lista)

#armazenando em variáveis:

a1, a2 = lista[0], lista[1]

#fazendo as operações que preciso e imprimindo o resultado:

resultado = (a1 - 1) + (a2 - 1)
print(resultado)

The problem is that I do not know how many numbers will be typed by the user, only that will be at least 1 and at 10. How do I do this without creating a condition for each case? I did this and the code became giant.

If there is a way to do this already with the lists, please let me know how.

  • Why do you need to do this? If you don’t know how many values will be entered, how will you know which variables will exist? You cannot use the list itself?

  • Not because I’m gonna have to do operations with the elements on that list. I need to decrease 1 of each element of the list and add the result and then print on the screen. Continuing the example of 2 items: result : (a1 - 1) + (a2 - 2) print(result)

  • But you can do that with the list...

  • @Andersoncarloswoss as?

  • It will be easier for you to ask the question exactly what you need to do. What you asked is looking like a XY problem

  • @Andersoncarloswoss will edit now

Show 1 more comment

2 answers

5


You will get the list with:

lista = input().split()

But we must remember that values will be strings. To subtract 1 from each value and then compute the sum, just use the function sum and of Generator comprehension:

resultado = sum(int(numero) - 1 for numero in lista)

If the entrance is 2 3 4, the result will be 6, referring to the sum (2-1)+(3-1)+(4-1).

  • I tried and it worked, thank you. I understood what you did but I have a question: , which is Generator comprehension ?

  • @Gustavomatias When you create a generator from the for inline.

  • I get it. There’s a way to show what the bow would look like for without being inline? I found examples on the internet but I can’t replicate in this situation

1

There is no point in storing each value of the list in different variables, remembering that a list is a vector of variables and has as one of the objectives precisely not having to declare N variables in the hand, there is no other way to do it but in the hand itself. The same meaning is to access the list values through the indices

lista = input().split()

#convertendo para tipo inteiro:

def int_converter(list_x):
    for i in range(len(list_x):
        list_x[i] = int(list_x[i])


int_converter(lista)

#Acessa todos os valores da lista e entrega de parametro para print

for i in range(len(lista)):
    print(lista[i])

#Ou...

for i in lista:
    print(i)

Edit: Since you commented on why you want to do this, it is still invalid to try to assign each element to a primitive type variable. Try to visualize the reason for the existence of vectors, in the case of your problem, something like this would solve:

def somaEstranha(lista):
     x = 0
     for i in range(len(lista)):
        x += lista[i] - i

     # Caso a primeira subtração tenha de ser por 1, 
     # basta fazer "x += lista[i] - (i + 1)"

    return x

Remember, every time an action in a code has become repetitive, it is a sign that it has to be replaced by only once the instruction within a loop, vectors serve for this assignment repetition case, too

  • I tried your solution and it didn’t work. To better understand what I wanted to do here is my full code and working: https://code.sololearn.com/cM11QSh7P3SS/#py link of the challenge I was trying to solve (the explanation): (https://olimpiada.ic.unicamp.br/pratique/p2/2007/f1/choc/)

Browser other questions tagged

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