Reading records from file and sorting

Francesc Altet faltet at carabos.com
Wed Nov 1 11:29:31 EST 2006


El dc 01 de 11 del 2006 a les 15:18 +0000, en/na George Sakkis va
escriure:
> > Why this is too specific or error-prone?
> 
> Because it
> 1. repeats the field types
> 2. requires adding up the length of all previous fields as offset.
> 
> If you're not convinced yet, try writing this in less than 3 seconds
> ;-):
> records = N.fromfile(a_file, dtype=N.dtype('i2,i4,f4,S5,B,Q'))
> records_by_f5 = ??

Ah, I see your point :)

> > I think your solution is quite good.If what you want is a more compact way to
> > write the above, you can
> > try with:
> >
> > In [56]:records=numpy.array([(1,1),(0,2)], dtype="i2,i4")
> > In [57]:records[records['f0'].argsort()]
> > Out[57]:
> > array([(0, 2), (1, 1)],
> >       dtype=[('f0', '<i2'), ('f1', '<i4')])
> > In [58]:records[records['f1'].argsort()]
> > Out[58]:
> > array([(1, 1), (0, 2)],
> >       dtype=[('f0', '<i2'), ('f1', '<i4')])
> 
> Ah, much better; I didn't know you can index a normal array (not
> recarray) by label. Now, if there's a way to do the sorting in place
> (records.sort('f1') doesn't work unfortunately), that would be perfect.

Yes, I agree that having the possibility to do records.sort('f1') would
be a great addition (both in terms of usability but also efficiency).

Cheers,

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list