在 Python 中返回相同种类但精度最高的标量类型
若要返回相同类型的最高精度标量类型,在 Python Numpy 中使用 maximum_sctype() 方法。参数是输入数据类型。它可以是 dtype 对象或可转换为 dtype 的对象。
步骤
首先,导入所需的库 −
import numpy as np
使用 Numpy 中的 maximum_sctype() 方法。获取最高精度的标量类型 −
print("Result...",np.maximum_sctype(int))
print("Result...",np.maximum_sctype(np.uint8))
print("Result...",np.maximum_sctype(complex))
print("Result...",np.maximum_sctype(str))
print("Result...",np.maximum_sctype('f4'))
print("Result...",np.maximum_sctype('i2'))
print("Result...",np.maximum_sctype('i4'))
print("Result...",np.maximum_sctype('i8'))范例
import numpy as np
# To return the scalar type of highest precision of the same kind as the input, use the maximum_sctype() method in Python Numpy
# The parameter is the input data type. This can be a dtype object or an object that is convertible to a dtype.
print("Using the maximum_sctype() method in Numpy\n")
# Get the scalar type of highest precision
print("Result...",np.maximum_sctype(int))
print("Result...",np.maximum_sctype(np.uint8))
print("Result...",np.maximum_sctype(complex))
print("Result...",np.maximum_sctype(str))
print("Result...",np.maximum_sctype('f4'))
print("Result...",np.maximum_sctype('i2'))
print("Result...",np.maximum_sctype('i4'))
print("Result...",np.maximum_sctype('i8'))输出
Using the maximum_sctype() method in Numpy Result... <class 'numpy.int64'> Result... <class 'numpy.uint64'> Result... <class 'numpy.complex256'> Result... <class 'numpy.str_'> Result... <class 'numpy.float128'> Result... <class 'numpy.int64'> Result... <class 'numpy.int64'> Result... <class 'numpy.int64'>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP