473,508 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting list and then return the index of the sorted item

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....

Jul 19 '05 #1
9 25837
custard_pie wrote:
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


http://aspn.activestate.com/ASPN/Coo.../Recipe/306862
Jul 19 '05 #2
Op 2005-05-03, custard_pie schreef <ck******@gmail.com>:
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]

--
Antoon Pardon

Jul 19 '05 #3
custard_pie wrote:
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]

Jul 19 '05 #4
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 :-)
Jul 19 '05 #5
On 3 May 2005 06:37:14 -0700, custard_pie <ck******@gmail.com> wrote:
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]

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jul 19 '05 #6
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

Jul 19 '05 #7
Op 2005-05-03, Antoon Pardon schreef <ap*****@forel.vub.ac.be>:
Op 2005-05-03, custard_pie schreef <ck******@gmail.com>:
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]

--
Antoon Pardon
Jul 19 '05 #8
Okay...THanks a lot everyone,.. Those codes really help....

Jul 19 '05 #9
Okay...THanks a lot everyone,.. Those codes are a real help....

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1734
by: Derek Basch | last post by:
Hello All, I need to sort a list using an unnatural sequence. I have a list like so: foo = print foo.sort()
19
25424
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
3214
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
4
4270
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
6
7191
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
2
1492
by: james_027 | last post by:
hi, are there available library or pythonic algorithm for sorting a list of list depending on the index of the list inside the list of my choice? d_list = , , , ,
5
3165
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
4
5306
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
5
4902
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.