NumPy数组(4)-- 自定义数组元素的数据类型dtype

一、自定义数组元素的数据类型概述

        根据不同的应用场景,需要自定义数据类型。类似于C语言中的自定义结构体。


二、自定义数组元素的数据类型

#NumPy数组-- 自定义数据类型
from numpy import *

#NumPy提供了那些数据类型
#int8 int16 int32 int64 float32(单精度)、float64或float(双精度)
#bool

a = array([['a',1,2],[3,4,5],[6,7,8]])
print(a)
print("******************************")

#定义表头,用来表示数组中的数据类型
t = dtype([('name',str_,20),('age',int8),('salary',float32)])

b = array([('a',1,2),(3,4,5),(6,7,8)],dtype=t)
print(b)
print("******************************")

items = array([('Bill',30,12345),('Mary',24,8000)])
print(items)
print("********************************")
items1 = array([('Bill',30,12345),('Mary',24,8000)],dtype=t)
print(items1)


输出结果:

输出结果:
[['a' '1' '2']
 ['3' '4' '5']
 ['6' '7' '8']]
******************************
[('a', 1,  2.) ('3', 4,  5.) ('6', 7,  8.)]
******************************
[['Bill' '30' '12345']
 ['Mary' '24' '8000']]
********************************
[('Bill', 30,  12345.) ('Mary', 24,   8000.)]




猜你喜欢

转载自blog.csdn.net/zhubao124/article/details/80601685