หากต้องการส่งคืนการแสดงสตริงของสเกลาร์ dtype ให้ใช้เมธอด sctype2char() ใน PythonNumpy อาร์กิวเมนต์ที่ 1 หากเป็น dtype สเกลาร์ อักขระสตริงที่เกี่ยวข้องจะถูกส่งคืน หากเป็นอ็อบเจกต์ sctype2char จะพยายามอนุมานประเภทสเกลาร์ จากนั้นส่งคืนอักขระสตริงที่เกี่ยวข้อง
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
การแสดงสตริงของประเภทสเกลาร์ -
for i in [np.int32, np.double, np.complex_, np.string_, np.ndarray]: print(np.sctype2char(i))
ส่งคืนการแสดงสตริงของประเภท int -
print("\nThe string representation of int types...")
for j in [np.int16, np.int32,np.int64]:
print(np.sctype2char(j)) ส่งคืนการแสดงสตริงของประเภท float -
print("\nThe string representation of float types...")
for j in [np.float16, np.float32,np.float64]:
print(np.sctype2char(j)) ตัวอย่าง
import numpy as np
# To return the string representation of a scalar dtype, use the sctype2char() method in Python Numpy
# The 1st argument, if a scalar dtype, the corresponding string character is returned.
# If an object, sctype2char tries to infer its scalar type and then return the corresponding string character.
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))
# Return the string representation of int types
print("\nThe string representation of int types...")
for j in [np.int16, np.int32,np.int64]:
print(np.sctype2char(j))
# Return the string representation of float types
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