ภาพรวม
ใช้ numpy.sctype2char() คืนค่าตัวอักษร (character) ที่แทน scalar dtype ถ้าเป็น scalar dtype จะคืน character ตรงกัน ถ้าเป็น object จะพยายาม infer scalar type แล้วคืน character
ขั้นตอนการทำงาน
1. นำเข้า NumPy
import numpy as np
2. แสดง string representation ของ scalar types ต่างๆ
for i in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
print(np.sctype2char(i))
3. String representation ของ int types
print("\nThe string representation of int types...")
for j in [np.int16, np.int32, np.int64]:
print(np.sctype2char(j))
4. String representation ของ float types
print("\nThe string representation of float types...")
for j in [np.float16, np.float32, np.float64]:
print(np.sctype2char(j))
ตัวอย่างโค้ดเต็ม
import numpy as np
print("The string representation of a scalar type...")
for i in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
print(np.sctype2char(i))
print("\nThe string representation of int types...")
for j in [np.int16, np.int32, np.int64]:
print(np.sctype2char(j))
print("\nThe string representation of float types...")
for j in [np.float16, np.float32, np.float64]:
print(np.sctype2char(j))
ผลลัพธ์
The string representation of a scalar type... i d D S O The string representation of int types... h i l The string representation of float types... e f d
ตารางสรุป:
| Type | Char |
|---|---|
| int16 | h |
| int32 | i |
| int64 | l |
| float16 | e |
| float32 | f |
| float64 | d |
| complex64 | F |
| complex128 | D |
| string_ | S |
| ndarray | O |