Python 如何在numpy中声明与此C结构类似的结构化数组类型?
我有以下c结构:Python 如何在numpy中声明与此C结构类似的结构化数组类型?,python,numpy,Python,Numpy,我有以下c结构: typedef struct { int blah[10]; } mine; 如何为此声明结构化numpy数组数据类型 我试过: mine_dtype = [ ('blah', [np.int32]) ] 但这不起作用 谢谢。在[267]中:我的\u数据类型=[ In [267]: mine_dtype = [ ...: ('blah', [n
typedef struct
{
int blah[10];
} mine;
如何为此声明结构化numpy数组数据类型
我试过:
mine_dtype = [
('blah', [np.int32])
]
但这不起作用
谢谢。在[267]中:我的\u数据类型=[
In [267]: mine_dtype = [
...: ('blah', [np.int32])
...: ]
In [268]: np.dtype(mine_dtype)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-268-6f0bb6a0df45> in <module>
----> 1 np.dtype(mine_dtype)
TypeError: data type not understood
In [269]: mine_dtype = [
...: ('blah', np.int32)
...: ]
In [270]: np.dtype(mine_dtype)
Out[270]: dtype([('blah', '<i4')])
In [271]: np.ones(3, dtype=mine_dtype)
Out[271]: array([(1,), (1,), (1,)], dtype=[('blah', '<i4')])
In [272]: _['blah']
Out[272]: array([1, 1, 1], dtype=int32)
…:('blah',[np.int32])
...: ]
In[268]:np.dtype(mine\u dtype)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1 np.数据类型(矿山数据类型)
TypeError:无法理解数据类型
在[269]中:mine\u dtype=[
…:('blah',np.int32)
...: ]
In[270]:np.dtype(mine\u dtype)
Out[270]:dtype([('blah','it not work'是什么意思?'it not work'?错误的定义,或者如何使用它?是的,我的意思是它是无效的dtype。你说的“声明一个结构化的numpy数组”是什么意思?在Python中无法声明类型。你只需创建一个numpy数组。例如np.array([0,0,0,0,0,0,0,0,0],dtype=np.int32)
您能详细介绍一下您想使用的用例吗?现在还不清楚您到底想做什么。请始终分享整个错误消息。我同意@lagerber的观点,我认为有更多信息会更好。
In [282]: mine_dtype = [
...: ('blah', np.int32, 10)
...: ]
In [283]: arr = np.zeros(3,mine_dtype)
In [284]: arr
Out[284]:
array([([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],),
([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],),
([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],)], dtype=[('blah', '<i4', (10,))])
In [285]: arr['blah'][:]=np.arange(10)
In [286]: arr
Out[286]:
array([([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],),
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9],)], dtype=[('blah', '<i4', (10,))])