[Numpy-discussion] structured array from ordinary array

Ernest Adrogué eadrogue@gmx....
Sun Dec 13 19:54:54 CST 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