Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ทดสอบว่าชนิด int ขนาดต่างๆ เป็น subdtype ของ integer class ใน Python

ในการทดสอบว่าชนิด int ขนาดต่างๆ เป็น subdtype ของ integer class ใช้เมธอด numpy.issubdtype() ใน NumPy พารามิเตอร์คือ dtype หรือออบเจ็กต์ที่บังคับให้เป็นหนึ่งได้

ขั้นตอน

ขั้นแรก ให้นำเข้าไลบรารี:

import numpy as np

ใช้ issubdtype() ตรวจสอบชนิดข้อมูลจำนวนเต็มขนาดต่างๆ:

print("Result...", np.issubdtype(np.int16, np.signedinteger))
print("Result...", np.issubdtype(np.int32, np.signedinteger))
print("Result...", np.issubdtype(np.int64, np.signedinteger))
print("Result...", np.issubdtype(np.int16, np.integer))
print("Result...", np.issubdtype(np.int32, np.integer))
print("Result...", np.issubdtype(np.int64, np.integer))

ตัวอย่างโค้ดเต็ม

import numpy as np
# ทดสอบว่า int ขนาดต่างๆ เป็น subdtype ของ integer class
# ใช้ numpy.issubdtype() ใน Python NumPy
# พารามิเตอร์คือ dtype หรือออบเจ็กต์ที่บังคับให้เป็นหนึ่งได้
print("Using the issubdtype() method in Numpy\n")
# ตรวจสอบชนิดข้อมูลจำนวนเต็มขนาดต่างๆ
print("Result...", np.issubdtype(np.int16, np.signedinteger))
print("Result...", np.issubdtype(np.int32, np.signedinteger))
print("Result...", np.issubdtype(np.int64, np.signedinteger))
print("Result...", np.issubdtype(np.int16, np.integer))
print("Result...", np.issubdtype(np.int32, np.integer))
print("Result...", np.issubdtype(np.int64, np.integer))

ผลลัพธ์

Using the issubdtype() method in Numpy
Result... True
Result... True
Result... True
Result... True
Result... True
Result... True