Help sorting a list by file extension

George Yoshida ml at dynkin.com
Thu Aug 11 22:17:03 EDT 2005


Bengt Richter wrote:
>>>>[name for dec,name in sorted((int(nm.split('.')[1]),nm) for nm in namelist)]
> 
> ['test.1', 'test.2', 'test.3', 'test.4', 'test.10', 'test.15', 'test.20']

Giving a key argument to sorted will make it simpler::

>>> sorted(namelist, key=lambda x:int(x.rsplit('.')[-1]))

-- george



More information about the Python-list mailing list