[Numpy-discussion] lexsort
Travis Oliphant
oliphant at ee.byu.edu
Wed May 3 22:39:11 EDT 2006
Charles R Harris wrote:
> Hi,
>
> On 5/2/06, *Francesc Altet* <faltet at carabos.com
> <mailto:faltet at carabos.com>> wrote:
>
> A Dimarts 02 Maig 2006 19:36, Charles R Harris va escriure:
> > Francesc,
> >
> > Completely off topic, but are you aware of the lexsort function
> in numpy
> > and numarray? It is like argsort but takes a list of
> (vector)keys and
> > performs a stable sort on each key in turn, so for record arrays
> you can
> > get the effect of sorting on column A, then column B, etc. I
> thought I
> > would mention it because you seem to use argsort a lot and,
> well, because I
> > wrote it ;)
>
> Thanks for pointing this out. In fact, I had no idea of this
> capability in numarray (numpy neither). I'll have to look more
> carefully into this to fully realize the kind of things that can be
> done with it. But it seems very promising anyway :-)
>
>
>
> As an example:
>
> In [21]: a
> Out[21]:
> array([[0, 1],
> [1, 0],
> [1, 1],
> [0, 1],
> [1, 0]])
>
> In [22]: a[lexsort((a[:,1],a[:,0]))]
> Out[22]:
> array([[0, 1],
> [0, 1],
> [1, 0],
> [1, 0],
> [1, 1]])
>
>
> Hmm, I notice that lexsort requires a tuple and won't accept a list. I
> wonder if there is a good reason for that. Travis?
>
Can't think of one right now except haste in coding...
-Travis
More information about the NumPy-Discussion
mailing list