[Baypiggies] sorting alphanumeric list

Tim Hatch tim at timhatch.com
Tue Jul 19 22:09:02 CEST 2011


> given the following list:
>
>>>> a
> ['Y6', 'T5']
>
> how do i modify list a to end up with:
>
>>>> b
> ['T5', 'Y6']
>
> of course my actual data consists of nested lists whose elements consist
> of
> lists like list a.

Maybe you could give an example involving A3, B1, and C20.  Are you trying
to sort on the numeric part or the letter?  Is the letter fixed size?

>>> a=['A3', 'B1', 'C20']
>>> a.sort(key=lambda i: int(i[1:]))
>>> a
['B1', 'A3', 'C20']

Tim



More information about the Baypiggies mailing list