简体   繁体   English

Python Z3B7F949B2343F9E5390​​E29F6EF5E1778Z 数组数据类型

[英]Python NumPy Array Data Type

import numpy as np  

d=np.dtype([('name',np.str_),('salary',np.int64)])  
arr = np.array([('Vedant',80000),('Subodh',4000)],dtype=d)  
print(arr)  

Output: Output:

[('', 80000) ('',  4000)]

Why are the strings blank?为什么字符串是空白的?

You need to set how many chars you want to insert.您需要设置要插入多少个字符。 ('name',np.str_, 4) this expect each word has only four chars. ('name',np.str_, 4)这期望每个单词只有四个字符。

import numpy as np  

d=np.dtype([('name',np.str_, 4),('salary',np.int64)])  
arr = np.array([('abcde',80000),('Subodh',4000)],dtype=d)  
print(arr)  
# [('abcd', 80000) ('Subo',  4000)]
# skip 'e' from 'abcde'
# skip 'dh' from 'Subodh'

You can define shape too.您也可以定义形状。

# we expect each array has shape=(2,2)
d = np.dtype([('name',np.str_, 4),('salary',np.int64, (2,2))])  
arr = np.array([('abcde',80000),('Subodh',[[10,20],[30,40]])],dtype=d)  
print(arr)   
# [('abcd', [[80000, 80000], [80000, 80000]]) # Because 80000 does not have shape=(2,2) item repeat to create array with shape=(2,2)
#  ('Subo', [[   10,    20], [   30,    40]])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM