sorting list and then return the index of the sorted item
Fuzzyman
fuzzyman at gmail.com
Tue May 3 09:57:48 EDT 2005
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
More information about the Python-list
mailing list