[Tutor] Original indices after Sorting

Ali Torkamani torkamani at gmail.com
Mon Jul 23 23:48:52 CEST 2012


On Mon, Jul 23, 2012 at 5:40 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> create a sorted copy
>
> L1 = [...original data...]
> L2 = sorted(L1)
>
> Now you can compare indexes except if there are duplicate elements
> and you need to know which specific copy is where. In that case
> I think you need to use id() but even that fails for small ints and other
> cached values.
>
> If the data is too large to hold two copies you could create a list
> of tuples with the original indexes:
>
> L2 = []
> n = -1
> while L1:
>    L2.append( (n,L1.pop()) )
>    n -= 1
>
> This creates L2 with a reversed L1 using the negative index of the
> original list. A bit clunky, I'm sure there is a neater way but it seems to
> work. But hopefully memory is not so tight you need to do this!
>

 sort_index = numpy.argsort(L1) solves it, and does not need such
Mumbo-Jumping!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120723/f9130311/attachment.html>


More information about the Tutor mailing list