[Numpy-discussion] Arrays as nested dtype

Warren Weckesser warren.weckesser at enthought.com
Fri Apr 8 22:52:37 EDT 2011


On Fri, Apr 8, 2011 at 9:23 PM, Robert Love <rblove_lists at comcast.net>wrote:

>
> Using np.loadtxt I can easily read my file that has columns of time, mode,
> 3 float64 for position and 3 for velocity like this.
>
> dt = dtype([('time', '|S12'),
> ('mode','|S3'),('rx','f8'),('ry','f8'),('rz','f8'),('vx','f8'),('vy','f8'),('vz','f8')])
>
> data = np.loadtxt('file', dtype=dt)
>
>
> I can then put the two pairs of 3 components into np.arrays and start
> performing the vector operations I need.
>
> How can I read them directly into np.arrays?
>
>
> dt = dtype([('time', '|S12'), ('mode','|S3'),np.array('r'), np.array('v')])
>
> I've seen examples for nested data that create a tuple but not an array.
>  Any tips  appreciated.
>


If you do this:

>>> dt = dtype([('time', '|S12'), ('mode','|S3'), ('r','f8', 3),
('v','f8',3)])
>>> data = np.loadtxt('file', dtype=dt)

then data['r'] and data['v'] are the arrays of positions and velocities.
You can then give them more convenient names:

>>> r = data['r']
>>> v = data['v']


Warren


> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110408/9727ab2e/attachment.html>


More information about the NumPy-Discussion mailing list