在 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