Convert numpy scalar to simple python type
numpy, python
Solution
In this case
import numpy as np
a = np.array(3)
b = np.array('3')
a_int = a.tolist()
b_str = b.tolist()
print type(a_int), type(b_str)
should work
Problem
I have a numpy array with a single value (scalar) which I would like to convert to correspoding Python data type. For example: ``` import numpy as np a = np.array(3) b = np.array('3') ``` I could convert them to `int` and `str` by casting: ``` a_int = int(a) b_str = str(b) ``` but I need to know the types in advance. I would like to convert `a` to an integer and `b` to a string without explicit type checking. Is there a simple way to achieve it?