list conversion question

Alex Martelli aleaxit at yahoo.com
Sun Sep 5 12:41:02 EDT 2004


Andrew Dalke <adalke at mindspring.com> wrote:
   ...
> In Python2.4 this is allowed
> 
>  >>> hist = [ 0, 1, 0, 5, 43 ]
>  >>> [pair[0] for pair in sorted(enumerate(hist),
> ...                             key=lambda pair: pair[1])]
> [0, 2, 1, 3, 4]
>  >>>
> 
> Not yet sure that that's a good thing.

it makes the fast (DSU) way of sorting easier, and can be sped up
further as:

[ p[0] for p in sorted(enumerate(hist), operator.itemgetter(1)) ]

The new itemgetter and attrgetter HOFs in module operator are quite good
for this kind of use (which is exactly why they got introduced).


Alex
 



More information about the Python-list mailing list