在 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP