[Numpy-discussion] is there a sortrows
josef.pktd@gmai...
josef.pktd@gmai...
Sun Dec 21 15:25:55 CST 2008
I was looking for a function that sorts a 2-dimensional array by rows.
That's what I came up with, is there a more direct way?
>>> a
array([[1, 2],
[0, 0],
[1, 0],
[0, 2],
[2, 1],
[1, 0],
[1, 0],
[0, 0],
[1, 0],
[2, 2]])
>>> a[np.lexsort(np.fliplr(a).T)]
array([[0, 0],
[0, 0],
[0, 2],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 2],
[2, 1],
[2, 2]])
Note: I needed to flip and transpose, using axis didn't work
>>> a.shape
(10, 2)
>>> np.lexsort(a,axis=1)
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
np.lexsort(a,axis=1)
ValueError: axis(=1) out of bounds
Specifying individual columns in argument also works, but it's a pain
if I don't know how many columns there are:
>>> a[np.lexsort((a[:,1],a[:,0]))]
array([[0, 0],
[0, 0],
[0, 2],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 2],
[2, 1],
[2, 2]])
A helper function sortrows would be helpful, I don't know what would
be the higher dimensional equivalent.
Or did I miss a function that I didn't find in the help file?
Thanks,
Josef
More information about the Numpy-discussion
mailing list