[Numpy-discussion] dtype and pep3118

Pauli Virtanen pav at iki.fi
Wed Nov 23 09:52:38 EST 2011


23.11.2011 15:01, Fabrice Silva kirjoitti:
> Hi folks,
> how should I specify a PEP3118 buffer format that could be understand by
> numpy as the following dtype:
> 
> dtype = [('t11', '|f8'), ('t22', '|f8'), ('t33', '|f8'),
>          ('t23', '|f8'), ('t13', '|f8'), ('t12', '|f8')]
> 
> so that I can manipulate a structured array and its fields ?
> 
> I tried strings like
> "T{d:t11: d:t22: d:t33: d:t23: d:t13: d:t12:}:Tensor2d:"
> "d:t11: d:t22: d:t33: d:t23: d:t13: d:t12:"
> 
> without success

>>> dtype = [('t11', '|f8'), ('t22', '|f8'), ('t33', '|f8'),
...          ('t23', '|f8'), ('t13', '|f8'), ('t12', '|f8')]
>>> x = np.zeros([1], dtype=dtype)
>>> memoryview(x).format
'T{d:t11:d:t22:d:t33:d:t23:d:t13:d:t12:}'
>>> np.array(memoryview(x))
array([(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)],
      dtype=[('t11', '<f8'), ('t22', '<f8'), ('t33', '<f8'), ('t23',
'<f8'), ('t13', '<f8'), ('t12', '<f8')])




More information about the NumPy-Discussion mailing list