[Numpy-discussion] 1D array sorting ascending and descending by fields
Patrick Redmond
plredmond at gmail.com
Mon Jun 4 14:10:44 EDT 2012
Here's how I sorted primarily by field 'a' descending and secondarily by
field 'b' ascending:
(Note that 'a' is the second column, 'b' is the first)
>>> data
array([('b', 0.03),
('c', 0.03),
('f', 0.03),
('e', 0.01),
('d', 0.04),
('a', 0.04)],
dtype=[('b', '|S32'), ('a', '<f8')])
>>> data.sort(order='b') # sort by b
>>> data = data[::-1] # reverse
>>> data[numpy.argsort(data['a'])][::-1] # sort by a and reverse
array([('a', 0.04),
('d', 0.04),
('b', 0.03),
('c', 0.03),
('f', 0.03),
('e', 0.01)],
dtype=[('b', '|S32'), ('a', '<f8')])
My question is whether there's an easier way to do this. Originally I
thought it would be possible to just do:
>>> data.sort(order=('-a', 'b'))
...indicating that the order of 'a' is descending, but this isn't part of
NumPy's sort behavior.
Your help is appreciated!
Thank you,
Patrick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120604/b24efe85/attachment.html>
More information about the NumPy-Discussion
mailing list