sorting a list numbers stored as strings

Michael J. Fromberger Michael.J.Fromberger at Clothing.Dartmouth.EDU
Mon Sep 24 09:08:43 EDT 2007


In article <1190632366.863711.100680 at y42g2000hsy.googlegroups.com>,
 aine_canby at yahoo.com wrote:

> hi,
> 
> I have the following list -
> 
> ["1", "11", "2", "22"]
> 
> how do I sort it like this -
> 
> ["1", "2", "11", "22"]
> 
> thanks,
> 
> aine

Try:

  lst.sort(key = lambda s: int(s))

Assuming, of course, that "lst" is your original list; this will sort it 
in place.  Also, you should probably read about the "sorted" function 
here:

 <http://docs.python.org/lib/built-in-funcs.html>

Cheers,
-M

-- 
Michael J. Fromberger             | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA



More information about the Python-list mailing list