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.... 9 25818
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
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
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]
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 :-)
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/
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
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
Okay...THanks a lot everyone,.. Those codes really help....
Okay...THanks a lot everyone,.. Those codes are a real help.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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()
|
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...
|
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...
|
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...
|
by: Arthur Dent |
last post by:
How do you sort a generic collection derived from
System.Collections.ObjectModel.Collection?
Thanks in advance,
- Arthur Dent
|
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 = ,
,
,
,
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |