python vector input

0

I have some input values of an array that will have the input of the following form:

1 2 3 4

2 3 4 0

So I'm trying to get the first row of m + n elements to turn everything into float, but it's not working.

aux = [0]*(m+n)

for i in range(m):
    aux = input()
    print(aux)
    aux = aux.split( )
    print(aux)
    map(float, aux)    

print(aux[1])
    
asked by anonymous 22.09.2017 / 20:53

2 answers

1

Okay, I think I understand what you need, so I'll really focus on the problem: a loop that reads% with% rows, in m format, separates this row into values and converts them to float , keeping such values in a list. For this, we do:

m = int(input("Quantas linhas? "))
n = int(input("Quantas colunas? "))

for i in range(m):
    while True:
        row = input()
        values = row.split(' ')

        if len(values) == n:
            break

    values = list(map(float, values))

    print(values)

The a b c d ... is to ensure that the for lines are read. m ensures that the entry always reads the expected number of columns, thus preventing the user from entering a different value - if the array has 3 columns, the while entry is invalid. I read the input, separated the values with 1 2 in the blanks and converted to float with the function split .

In other words, your code was on the right track, but you must have rolled over the returns of the function, remembering to assign the return to a variable. It is worth remembering that the return of map is a generator, so I used map to convert it to a list.

    
22.09.2017 / 21:39
0

I was able to solve the problem. But I realized that my problem was to get the answer from map

m = int(input())
n = int(input())

A = []
for i in range(m):
    A.append( [0] *(m+n))

list = [0]*(n)
new_list = []
List_copy = [0]*(m*n)

for i in range(m):
    lista = input()
    #print(lista)
    lista = lista.split(' ')

    for item in lista:
        new_list.append(float(item))
        #print(new_list)

numpyList = np.asarray(new_list)
A = np.reshape(numpyList, (m,n))
print(A)
    
23.09.2017 / 00:18