Python Vector Fill 3

Asked

Viewed 1,508 times

1

entrada1 = input()
entrada1 = entrada1.split(" ")
entrada1 = [float(numero) for numero in entrada1]

Can anyone explain what’s happening on line 3?

3 answers

1

I’m new to Python, but I think I’m turning the values of numero1 into float and playing an array

[float(number) for numero in entrada1]

And assigned the array above in1, which were previously strings.

The more experienced correct me please, but it must be some more simplified way to implement:

entrada2 = []
for i in range(0, len(entrada1)):
   entrada2.append(float(entrada1[i]));

entrada1 = entrada2

0

You can use the following algorithm...

entrada = list(map(float, input().split()))
print(f'\033[32mO resultado é: {entrada}')

See how the algorithm works on repl it..

Note that when we run this program, the tela is clean, waiting for the values that will be typed. From that moment on we must all the desired values, in the same line, separated by a space and then press enter.

Note that this program creates a list of size indefinido, that is, if you insert 5 values, the list will have 5 elements. If you insert 1000 values, the list will have 1000 elements.

0

entrada1 = [float(numero) for numero in entrada1]

entra1 = entrada1 will receive the following content

[....] a vector

float(numero) this 'parsing' number on a float or is transforming the content of number into a floating point number (a decimal number)

for numero in entrada for each input element will reference it as number

if you have ['1','5','3.1']

first interaction number = '1'

second interaction number = '5'

third interaction number = '3.1'

in the end would be like

entrada1 = [1.0, 5.0, 3.1]

Browser other questions tagged

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