On 23/07/12 20:55, Ali Torkamani wrote:
Hi every one,
How can we get the indices of values in the original list after sorting
a list?

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!


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to