473,661 Members | 2,440 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 25876
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
1744
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
25451
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
3232
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
4279
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
7205
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
3179
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
5319
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
4933
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
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
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
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
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...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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
4177
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...
1
2760
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

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.