While we're talking about annoyances

Michael Hoffman cam.ac.uk at mh391.invalid
Sun Apr 29 12:46:05 CEST 2007

GHUM wrote:
> Steven,
>> def index(sequence):
>>     decorated = zip(sequence, xrange(len(sequence)))
>>     decorated.sort()
>>     return [idx for (value, idx) in decorated]
> would'nt that be equivalent code?
> def index(sequence):
>     return [c for _,c  in sorted((b,a) for a, b in
> enumerate(sequence))]

Or even these:

def index(sequence):
     return sorted(range(len(sequence)), key=sequence.__getitem__)

def rank(sequence):
     return sorted(range(len(sequence)),

Hint: if you find yourself using a decorate-sort-undecorate pattern, 
sorted(key=func) or sequence.sort(key=func) might be a better idea.
Michael Hoffman

More information about the Python-list mailing list