Pythonic list reordering

Gary Herron gherron at islandtraining.com
Fri Apr 9 00:59:34 CEST 2010


Ben Racine wrote:
> I have a list...
>
> ['dir_0_error.dat', 'dir_120_error.dat', 'dir_30_error.dat', 'dir_330_error.dat']
>
> I want to sort it based upon the numerical value only.
>
> Does someone have an elegant solution to this? 
>
> Thanks,
> Ben R.
>   

How about a one liner?

    L.sort(key=lambda s: int(s.split('_')[1]))


(Which is not necessarily elegant, but it is short.)

Gary Herron




More information about the Python-list mailing list