numpy.can_cast() วิธีการคืนค่า True หากการส่งข้อมูลระหว่างประเภทข้อมูลสามารถเกิดขึ้นได้ตามกฎการแคสต์ พารามิเตอร์ที่ 1 คือชนิดข้อมูลหรืออาร์เรย์ที่จะส่ง พารามิเตอร์ตัวที่ 2 คือประเภทข้อมูลที่จะส่งไป
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การใช้ can_cast() เพื่อตรวจสอบว่าการส่งข้อมูลระหว่างประเภทข้อมูลสามารถเกิดขึ้นตามกฎการแคสต์ได้หรือไม่ -
print("Checking with can_cast() method in Numpy\n")
print("Result...",np.can_cast(np.int32, np.int64))
print("Result...",np.can_cast(np.float64, complex))
print("Result...",np.can_cast(complex, float))
print("Result...",np.can_cast('i8', 'f8'))
print("Result...",np.can_cast('i8', 'f4'))
print("Result...",np.can_cast('i4', 'S4')) ตัวอย่าง
import numpy as np
# The numpy.can_cast() method returns True if cast between data types can occur according to the casting rule.
# The 1st parameter is the data type or array to cast from.
# The 2nd parameter is the data type to cast to.
print("Checking with can_cast() method in Numpy\n")
print("Result...",np.can_cast(np.int32, np.int64))
print("Result...",np.can_cast(np.float64, complex))
print("Result...",np.can_cast(complex, float))
print("Result...",np.can_cast('i8', 'f8'))
print("Result...",np.can_cast('i8', 'f4'))
print("Result...",np.can_cast('i4', 'S4')) ผลลัพธ์
Checking with can_cast() method in Numpy Result... True Result... True Result... False Result... True Result... False Result... False