numpy.promote_types() วิธีการส่งกลับชนิดข้อมูลที่มีขนาดที่เล็กที่สุดและ scalarkind ที่เล็กที่สุดซึ่งทั้ง type1 และ type2 อาจถูกส่งอย่างปลอดภัย ส่งกลับชนิดข้อมูลที่เลื่อนระดับ ชนิดข้อมูลที่กลับมาจะอยู่ในลำดับไบต์ดั้งเดิมเสมอ พารามิเตอร์ที่ 1 เป็นประเภทข้อมูลแรก พารามิเตอร์ที่ 2 เป็นประเภทข้อมูลที่สอง
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การตรวจสอบด้วยเมธอด promote_types() ใน Numpy -
print("Result...",np.promote_types('f4', 'f8'))
print("Result...",np.promote_types('i8', 'f4'))
print("Result...",np.promote_types('>i8', '<c8'))
print("Result...",np.promote_types('i4', 'S8'))
print("Result...",np.promote_types(np.int32, np.int64))
print("Result...",np.promote_types(np.float64, complex))
print("Result...",np.promote_types(complex, float)) ตัวอย่าง
import numpy as np
# The numpy.promote_types() method returns the data type with the smallest size and smallest scalar kind to which both type1 and type2 may be safely cast.
print("Checking with promote_types() method in Numpy\n")
print("Result...",np.promote_types('f4', 'f8'))
print("Result...",np.promote_types('i8', 'f4'))
print("Result...",np.promote_types('>i8', '<c8'))
print("Result...",np.promote_types('i4', 'S8'))
print("Result...",np.promote_types(np.int32, np.int64))
print("Result...",np.promote_types(np.float64, complex))
print("Result...",np.promote_types(complex, float)) ผลลัพธ์
Checking with promote_types() method in Numpy Result... float64 Result... float64 Result... complex128 Result... |S11 Result... int64 Result... complex128 Result... complex128