python - Get numpy 2D array from user input -



python - Get numpy 2D array from user input -

i trying numpy two-dimensional array user input except not work input() returns 'str' type while numpy array() method expects tuple:

import numpy n def main(): = input() # user expected come in 2d array [[1,2],[3,4]] illustration = n.array(a) # since 'str' print(a.shape) # output '()' instead of '(2, 2)' showing previous instruction didn't work expected if __name__ == "__main__": main()

so question be: how can turn input string tuple array() method turns input numpy 2d array?

thanks in advance.

the next interactive session worked fine in case:

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

are entering info wrapped in quotes, i.e.:

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

as that's reason can think of code fail. input not homecoming string unless user types 1 in; equivalent eval(raw_input()) in case.

python numpy user-input

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -