1

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.

1
  • You will have to manually iterate through the string and get the numbers out in tuple form to create the array. Commented Oct 6, 2014 at 15:33

7 Answers 7

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"Input does not return a string unless the user types one in": not true in Python 3.
Thanks for letting me know, it's a long time since I've used anything input() related.
2

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'>

Comments

2

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)

Comments

0

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())

Comments

0
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)

Comments

0

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

1 Comment

It may be a great answer but without an explanation of how you arrived at it, it may just turn out to not be useful to others
-1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.