Computer >> Máy Tính >  >> Lập trình >> Python

Trả về True nếu đối số đầu tiên là một mã kiểu chữ thấp hơn / bằng trong hệ thống phân cấp kiểu trong Python

Để trả về True nếu đối số đầu tiên là mã kiểu thấp hơn / bằng trong phân cấp kiểu, hãy sử dụng phương thức thenumpy.issubdtype () trong Python Numpy. Các tham số là loại dtype hoặc đối tượng có thể cưỡng chế để loại bỏ

Các bước

Đầu tiên, hãy nhập thư viện được yêu cầu -

import numpy as np

Sử dụng phương thức Issubdtype () trong Numpy -

print("Result...",np.issubdtype(np.float64, np.float32))
print("Result...",np.issubdtype(np.float64, np.floating))
print("Result...",np.issubdtype(np.float32, np.floating))
print("Result...",np.issubdtype('i4', np.signedinteger))
print("Result...",np.issubdtype('i8', np.signedinteger))
print("Result...",np.issubdtype(np.int32, np.integer))

Ví dụ

import numpy as np

# To return True if first argument is a typecode lower/equal in type hierarchy, use the numpy.issubdtype() method in Python Numpy.

# The parameters are the dtype or object coercible to one
print("Using the issubdtype() method in Numpy\n")
print("Result...",np.issubdtype(np.float64, np.float32))
print("Result...",np.issubdtype(np.float64, np.floating))
print("Result...",np.issubdtype(np.float32, np.floating))
print("Result...",np.issubdtype('i4', np.signedinteger))
print("Result...",np.issubdtype('i8', np.signedinteger))
print("Result...",np.issubdtype(np.int32, np.integer))

Đầu ra

Using the issubdtype() method in Numpy

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