返回Python中两种给定类型可以安全转换到的最小大小和标量类型的的数据类型
numpy.promote_types() 方法返回最小大小和最小标量类型的数据类型,type1 和 type2 可以安全地转换为该类型。返回提升后的数据类型。返回的数据类型始终为本地字节序。第一个参数是第一个数据类型。第二个参数是第二个数据类型。
步骤
首先,导入所需的库:
import numpy as np
使用 NumPy 中的 promote_types() 方法进行检查:
print("Result...",np.promote_types('f4', 'f8'))
print("Result...",np.promote_types('i8', 'f4'))
print("Result...",np.promote_types('>i8', '<c8'))
print("Result...",np.promote_types('i4', 'S8'))
print("Result...",np.promote_types(np.int32, np.int64))
print("Result...",np.promote_types(np.float64, complex))
print("Result...",np.promote_types(complex, float))示例
import numpy as np
# The numpy.promote_types() method returns the data type with the smallest size and smallest scalar kind to which both type1 and type2 may be safely cast.
print("Checking with promote_types() method in Numpy\n")
print("Result...",np.promote_types('f4', 'f8'))
print("Result...",np.promote_types('i8', 'f4'))
print("Result...",np.promote_types('>i8', '<c8'))
print("Result...",np.promote_types('i4', 'S8'))
print("Result...",np.promote_types(np.int32, np.int64))
print("Result...",np.promote_types(np.float64, complex))
print("Result...",np.promote_types(complex, float))输出
Checking with promote_types() method in Numpy Result... float64 Result... float64 Result... complex128 Result... |S11 Result... int64 Result... complex128 Result... complex128
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP