[Numpy-discussion] structured array from ordinary array

Ernest Adrogué eadrogue at gmx.net
Sun Dec 13 20:54:54 EST 2009


Hi,

How does one generate a structured array from a normal
array?

I use the 'view' method, but this way I get a superfluous
dimension that I do not want. Example:

In [619]: a = np.array([[1,2,3],[1,2,3],[1,2,4]],int)

In [620]: struct = np.dtype([('a',int),('b',int),('c',int)])

In [621]: a.view(struct)
Out[621]: 
array([[(1, 2, 3)],
       [(1, 2, 3)],
       [(1, 2, 4)]], 
      dtype=[('a', '<i8'), ('b', '<i8'), ('c', '<i8')])

In [622]: a.view(struct)['a']
Out[622]: 
array([[1],
       [1],
       [1]])

In [623]: a.view(struct)['a'].shape
Out[623]: (3, 1)

I'd like the last array to have shape (3,).
What am I doing wrong??

Ernest



More information about the NumPy-Discussion mailing list