On Sun, Dec 21, 2008 at 20:53, David Cournapeau
<[email protected]> wrote:
> [email protected] wrote:
>> 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?
>
> I may miss something obvious, but why are you using lexsort at all ? At
> leat, the first example is easily achieved with sort(x, axis=0)
No, it isn't.
In [4]: sort(a, axis=0)
Out[4]:
array([[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 1],
[1, 2],
[2, 2],
[2, 2]])
Compare to his desired result:
array([[0, 0],
[0, 0],
[0, 2],
[1, 0],
[1, 0],
[1, 0],
[1, 0],
[1, 2],
[2, 1],
[2, 2]])
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
_______________________________________________
Numpy-discussion mailing list
[email protected]
http://projects.scipy.org/mailman/listinfo/numpy-discussion