473,698 Members | 1,921 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 25882
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(enumerat e(v), key=operator.it emgetter(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(enumerat e(lst), key=operator.it emgetter(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(enumerat e(spam), key=lambda

item: item[1]))
[2, 0, 1, 3, 4]

--
Cheers,
Simon B,
si***@brunningo nline.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
1746
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
25459
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 sort columns based on the column header the user has clicked in both Ascending and Descending formats.
0
3237
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 way... here is my code on how I did it to help anyone starting out get an idea of how to use virtual mode in ..NET 2.0 Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared
4
4281
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 is stored in a double linked list, the whole list is sorted for the next round. My problem appears when subjecting the program to heavy load, that is, when I run the simulation for more than 10,000 miliseconds (every event occurs in...
6
7211
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
1508
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
3184
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 search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
4
5321
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 (Sorts.java): import java.util.*; /**
5
4946
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
8671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.