返回对象的Python类型的标量数据类型或NumPy等效类型
要返回对象的Python类型的标量数据类型或NumPy等效类型,请使用numpy.obj2sctype()方法。第一个参数是要返回其类型的对象。如果提供了默认参数,则返回无法确定其类型的对象。如果没有给出,则为这些对象返回None。
步骤
首先,导入所需的库 -
import numpy as np
要返回对象的 Python 类型 的标量数据类型或 NumPy 等效类型,请使用 numpy.obj2sctype() 方法 -
print("Using the obj2sctype() method in Numpy
")
检查int -
print("Result...",np.obj2sctype(np.array([45, 89]))) print("Result...",np.obj2sctype(np.array([389, 7985])))
检查float -
print("Result...",np.obj2sctype(np.float32)) print("Result...",np.obj2sctype(np.float64)) print("Result...",np.obj2sctype(np.array([5., 25., 40.])))
检查complex -
print("Result...",np.obj2sctype(np.array([5.6j]))
示例
import numpy as np # To return the scalar dtype or NumPy equivalent of Python type of an object, use the numpy.obj2sctype() method. # The 1st parameter is the object of which the type is returned # The default parameter, if given, is returned for objects whose types cannot be determined. # If not given, None is returned for those objects. print("Using the obj2sctype() method in Numpy
") # checking for int print("Result...",np.obj2sctype(np.array([45, 89]))) print("Result...",np.obj2sctype(np.array([389, 7985]))) # checking for float print("Result...",np.obj2sctype(np.float32)) print("Result...",np.obj2sctype(np.float64)) print("Result...",np.obj2sctype(np.array([5., 25., 40.]))) # checking for complex print("Result...",np.obj2sctype(np.array([5.6j])))
输出
Using the obj2sctype() method in Numpy Result... <class 'numpy.int64'> Result... <class 'numpy.int64'> Result... <class 'numpy.float32'> Result... <class 'numpy.float64'> Result... <class 'numpy.float64'> Result... <class 'numpy.complex128'>
广告