返回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

更新於:2022年2月25日

82 次瀏覽

開啟您的職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.