[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) - but maybe you have more complicated examples in mind where you need actual lexical sort: David _______________________________________________ Numpy-discussion mailing list [email protected] http://projects.scipy.org/mailman/listinfo/numpy-discussion
