index of items in a List in sorted order
Nizar Batada
nbatada1 at yahoo.com
Sun Nov 24 21:41:01 EST 2002
I am posting a code to get index of sorted list for
future reference.
if you can make this code faster, i'll appreciate your input
nizar
########################################################3
#!/usr/bin/python
def ind_of_sorted(L):
''' return index of items in sorted order '''
k = len(L)
d = {}
# take care of repeated values
[ d.setdefault(L[i],[]).append(i) for i in range(k)]
auxL = d.keys()
auxL.sort()
inds = []
[inds.extend( d[x] ) for x in auxL]
return auxL,inds
if __name__ == '__main__':
L=['r','g','a','f','r','j']
Lsort,Lsort_I=ind_of_sorted(L)
print L
print Lsort
print Lsort_I
More information about the Python-list
mailing list