簡體   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:

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

為什么字符串是空白的?

您需要設置要插入多少個字符。 ('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'

您也可以定義形狀。

# 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