Pythonic list reordering
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?
> Ben R.
How about a one liner?
L.sort(key=lambda s: int(s.split('_')))
(Which is not necessarily elegant, but it is short.)
More information about the Python-list