在 Python 中判斷第一個引數中的型別是否是第二個引數的子類


在 Python 中使用 numpy.issubsctype() 方法,判斷第一個引數中的型別是否是第二個引數的子類。第一個和第二個引數是資料型別。

步驟

首先,匯入所需的庫 −

import numpy as np

使用 Numpy 中的 issubsctype() 方法。檢查第一個引數是否是第二個引數的子類 −

print("Result...",np.issubsctype(np.float16, np.float32))
print("Result...",np.issubsctype(np.int32, np.signedinteger))
print("Result...",np.issubsctype('i4', np.signedinteger))
print("Result...",np.issubsctype('S8', str))
print("Result...",np.issubsctype(np.array([45, 89]), int))
print("Result...",np.issubsctype(np.array([5., 25., 40.]), float))

示例

import numpy as np

# To determine if the type in the first argument is a subclass of second, use the numpy.issubsctype() method in Python numpy
# The 1st and the 2nd argument are datatypes

print("Using the issubsctype() method in Numpy\n")

# Checking whether the first argument is a subclass of the second argument
print("Result...",np.issubsctype(np.float16, np.float32))
print("Result...",np.issubsctype(np.int32, np.signedinteger))
print("Result...",np.issubsctype('i4', np.signedinteger))
print("Result...",np.issubsctype('S8', str))
print("Result...",np.issubsctype(np.array([45, 89]), int))
print("Result...",np.issubsctype(np.array([5., 25., 40.]), float))

輸出

Using the issubsctype() method in Numpy

Result... False
Result... True
Result... True
Result... False
Result... True
Result... True

更新時間: 2022 年 2 月 24 日

117 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.