sorting list and then return the index of the sorted item

C

custard_pie

I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
Can someone help please....
 
A

Antoon Pardon

Op 2005-05-03 said:
I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
Can someone help please....

Something like this:
lst = [2,3,1,4,5]
inx = range(len(lst))
inx.sort(lambda x,y: lst[x] - lst[y])
print inx
[2, 0, 1, 3, 4]
 
D

Duncan Booth

custard_pie said:
I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
Can someone help please....

you need to pair up your values with the list indices, sort the list of
pairs then strip out the indices.

One way to do it:
v = [2, 3, 1, 4, 5]
import operator
[ i for (i,j) in sorted(enumerate(v), key=operator.itemgetter(1))]
[2, 0, 1, 3, 4]
 
A

Antoon Pardon

Op 2005-05-03 said:
Op 2005-05-03 said:
I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
Can someone help please....

Something like this:
lst = [2,3,1,4,5]
inx = range(len(lst))
inx.sort(lambda x,y: lst[x] - lst[y])
print inx
[2, 0, 1, 3, 4]

Something a bit more usefull in general:

lst = [2,3,1,4,5]
inx = range(len(lst))
inx.sort(lambda x,y: cmp(lst[x],lst[y]))
print inx
[2, 0, 1, 3, 4]
 
F

F. Petitjean

Le 3 May 2005 06:37:14 -0700, custard_pie a écrit :
I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
Can someone help please....
If you have Python version 2.4 (or 2.4.1):
lst = [2,3,1,4,5]
import operator
sorted(enumerate(lst), key=operator.itemgetter(1))
[(2, 1), (0, 2), (1, 3), (3, 4), (4, 5)]

But *why* do you want the indices ? Most of the time dealing with the
indices is the wrong way in Python (perhaps the right way in FORTRAN :)
 
S

Simon Brunning

I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
spam = [2,3,1,4,5]
list(index for index, item in sorted(enumerate(spam), key=lambda
item: item[1]))
[2, 0, 1, 3, 4]
 
F

Fuzzyman

I'd map the values to their index in a dictionary, then sort the list,
and from the sorted list fetch all the indexes from the dictionary.
Something like :
a = [2,3,1,4,5]
b = list(a)
b.sort()
b [1, 2, 3, 4, 5]
indexDict = dict([ (value, index) for index, value in enumerate(a)])
[indexDict[entry] for entry in b]
[2, 0, 1, 3, 4]

HTH

The code above uses enumerate, to shortcut getting the index. There may
be other shortcuts possible.

Regards,

Fuzzy
http://www.voidspace.org.uk/python
 
S

Scott David Daniels

Duncan said:
custard_pie said:
I need help sorting a list...I just can't figure out how to sort a list
and then return a list with the index of the sorted items in the list
for example if the list I want to sort is [2,3,1,4,5]
I need [2,0,1,3,4] to be returned
...
v = [2, 3, 1, 4, 5]
import operator
[ i for (i,j) in sorted(enumerate(v), key=operator.itemgetter(1))]

[2, 0, 1, 3, 4]

Or, to get fancy with Python 2.4:

result = sorted(range(len(v)), key=v.__getitem__)

Make a list of indices, and says the sort key is "over there" to sorted.

--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top