obento
obento

Reputation: 11

Get numpy 2D array from user input

I am trying to get a numpy two-dimensional array from user input except it does not work properly as input() returns a 'str' type while the numpy array() method expects a tuple:

import numpy as N

def main():
    A = input()       # the user is expected to enter a 2D array like [[1,2],[3,4]] for example
    A = N.array(A)    # since A is a 'str' A
    print(A.shape)    # output is '()' instead of '(2, 2)' showing the previous instruction didn't work as expected

if __name__ == "__main__":
main()

So my question would be: How can I turn the input string into a tuple so that the array() method properly turns the input into a numpy 2D array?

Thanks in advance.

Upvotes: 1

Views: 20176

Answers (7)

sshivanshu992
sshivanshu992

Reputation: 1

Numpy 2D array user defined input

# Importing the NumPy library for numerical computations
import numpy as np

lis = []  # Initializing an empty list to store user input values

# Looping three times to get input for three different values
for i in range(3):
    # Prompting the user to enter a value and splitting it into separate values
    # based on the current iteration index i
    user_input = input(f"Enter the value {[i]}:").split()

    # Appending the entered values to the list
    lis.append(user_input)

    # Converting the list of entered values to a NumPy array with integer data type
    arr = np.array(lis, dtype=int)

print(arr)  # Printing the final NumPy array containing the entered values

Upvotes: 0

sshivanshu992
sshivanshu992

Reputation: 1

import numpy as np
arr = np.array([[int(x) for x in input(f"Enter the value{[i]}:").split()] for i in range(3)])

print(arr)

Upvotes: 0

Nik_Coding
Nik_Coding

Reputation: 1

Or simply you can just do this:

import numpy

def array_input():
    a = eval(input('Enter list: '))
    b = numpy.array(a)
return b
print(array_input())

Upvotes: 0

Kevin Bill
Kevin Bill

Reputation: 31

Brett Lempereur and Sidharth Shah's answers is just for Python2 because input() in Python3 will return str by default.

In Python3, you would also need to use ast to eval string input to list.

>>> import ast
>>> import numpy as np
>>> matrixStr = input()
[[1, 2], [3, 4]]
>>> type(matrixStr)
str
>>> matrixList = ast.literal_eval(matrixStr)
>>> type(matrixList)
list
>>> matrix = np.array(matrix)

Upvotes: 2

Md Shahid
Md Shahid

Reputation: 1

to get an integer input you have to pass input() within int() like...

A=int(input())
type(A)
>>>int

Upvotes: -1

Sidharth Shah
Sidharth Shah

Reputation: 1609

I think the code is correct you need to change the way you input data. Please look at following code snippet

>>> import numpy as N
>>> A = input()
((1,2),(3,4))
>>> type(A[0][0])
<type 'int'>

Upvotes: 2

Brett Lempereur
Brett Lempereur

Reputation: 815

The following interactive session worked fine in my case:

>>> A = input()
[[1, 2], [3, 4]]
>>> 
>>> A
[[1, 2], [3, 4]]
>>> type(A)
<type 'list'>
>>> import numpy
>>> numpy.array(A)
array([[1, 2],
      [3, 4]])
>>> 

Are you entering the data wrapped in quotes, i.e.:

>>> A = input()
"[[1, 2], [3, 4]]"
>>> A
'[[1, 2], [3, 4]]'
>>> type(A)
<type 'str'>

As that's the only reason I can think of for your code to fail. Input does not return a string unless the user types one in; it is equivalent to eval(raw_input()) in your case.

Upvotes: 2

Related Questions