How to ask the user to create an np.array vector?

Asked

Viewed 74 times

-1

I would like to know HOW is it possible to write a program that reads an array numpy vector np.array([]) user-created.

In lists, for example, we do:

n = int(input("Determine o número elementos da lista: "))
i = 0
lista = []
while i < n:
  elemento = int(input("Digite os elementos da lista: "))
  i += 1
  lista.append(elemento)
print(lista)

In the case of nummpy array vectors, how would this be done without me having to write a program that first receives a user list and then does:vetor = np.array([lista])?

It has how to write a program that allows the user to make his own vector?

1 answer

0

Import of numpy

import numpy as np

Creating the np array

a = np.array([])

Adding elements, you can do this with while, with an input only and mapping the entries, here follows an example with for:

for i in range(5):
    a = np.append(a, float(input('n: ')), axis = None)

You can change the casting to integer if it is only integers.

Entree:

n: 1
n: 2
n: 3
n: 4
n: 5

Exit:

print(a)
[1. 2. 3. 4. 5.]

Object type:

type(a)
numpy.ndarray

Browser other questions tagged

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