sorting list and then return the index of the sorted item

Simon Brunning simon.brunning at gmail.com
Tue May 3 09:55:54 EDT 2005


On 3 May 2005 06:37:14 -0700, custard_pie <ckwiguno at 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,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list