list conversion question

Peter Otten __peter__ at web.de
Sat Sep 4 14:13:21 EDT 2004


Jp Calderone wrote:

>    Even faster, though perhaps not as clear:
> 
>      >>> import operator
>      >>> hist = [ 0, 1, 0, 5, 43 ]
>      >>> map(operator.itemgetter(0), sorted(enumerate(hist),
>      ...                             key=operator.itemgetter(1)))
>      [0, 2, 1, 3, 4]
 
Yet another option, not benchmarked, perhaps clearer:

>>> hist = [0, 1, 0, 5, 43]
>>> indices = range(len(hist))
>>> indices.sort(key=hist.__getitem__)
>>> indices
[0, 2, 1, 3, 4]

Peter




More information about the Python-list mailing list