RE: sorting a list numbers stored as strings

2007-09-26 Thread Delaney, Timothy (Tim)
Hrvoje Niksic wrote: > "Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes: > >> Yep - appears I must have been misremembering from another language >> (dunno which) > > Tcl Not bloody likely - only used Tcl for expect, and then only very minimally. I'm sure there's at least one language though

RE: sorting a list numbers stored as strings

2007-09-26 Thread Delaney, Timothy (Tim)
ZeD wrote: > thebjorn wrote: > >> int("020") >>> 20 >> 020 >>> 16 >> >> You can get the latter behavior using eval: > > why using eval when int has the "base" optional parameter? > int("020") > 20 int("020", 8) > 16 int("09", 8) > Traceback (most recent call last): > F

Re: sorting a list numbers stored as strings

2007-09-25 Thread ZeD
thebjorn wrote: >> >>> int("020") >> 20 >> >>> 020 >> 16 > > You can get the latter behavior using eval: why using eval when int has the "base" optional parameter? >>> int("020") 20 >>> int("020", 8) 16 >>> int("09", 8) Traceback (most recent call last): File "", line 1, in ValueError: inval

Re: sorting a list numbers stored as strings

2007-09-25 Thread thebjorn
On Sep 25, 2:45 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Tue, 25 Sep 2007 12:46:54 +0800, Delaney, Timothy (Tim) wrote: > > Carsten Haese wrote: > > >> On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: > >>> I'm sure that in some version of Python it wou

Re: sorting a list numbers stored as strings

2007-09-25 Thread Hrvoje Niksic
"Delaney, Timothy (Tim)" <[EMAIL PROTECTED]> writes: > Yep - appears I must have been misremembering from another language > (dunno which) Tcl -- http://mail.python.org/mailman/listinfo/python-list

Re: sorting a list numbers stored as strings

2007-09-25 Thread Steven D'Aprano
On Tue, 25 Sep 2007 12:46:54 +0800, Delaney, Timothy (Tim) wrote: > Carsten Haese wrote: > >> On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: >>> I'm sure that in some version of Python it would have given a >>> ValueError (due to the default radix being 0) but it appears to have

Re: sorting a list numbers stored as strings

2007-09-24 Thread Amit Khemka
On 9/24/07, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote: > > On 9/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > > > >>> l = ["1", "11", "2", "22"] > > >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your > > own compare

RE: sorting a list numbers stored as strings

2007-09-24 Thread Delaney, Timothy (Tim)
Carsten Haese wrote: > On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: >> I'm sure that in some version of Python it would have given a >> ValueError (due to the default radix being 0) but it appears to have >> changed to a default radix of 10 somewhere along the way. > > Not eve

Re: sorting a list numbers stored as strings

2007-09-24 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] 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 origi

RE: sorting a list numbers stored as strings

2007-09-24 Thread Carsten Haese
On Mon, 2007-09-24 at 19:58 +0800, Delaney, Timothy (Tim) wrote: > I'm sure that in some version of Python it would have given a > ValueError (due to the default radix being 0) but it appears to have > changed to a default radix of 10 somewhere along the way. Not even Python 1.5.2 seems to have a

Re: sorting a list numbers stored as strings

2007-09-24 Thread Peter Otten
aine_canby wrote: > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] >>> items = ["1", "11", "2", "22"] >>> items.sort(key=int) >>> items ['1', '2', '11', '22'] This is more efficient than Amit's compare function and even Bruno

RE: sorting a list numbers stored as strings

2007-09-24 Thread Delaney, Timothy (Tim)
Carsten Haese wrote: > That interpreter session is a work of fiction, since sorted returns > the sorted list instead of sorting the list in place. Also, it's > better (i.e. more readable and likely faster) to use a sort key > function instead of a comparison function whenever possible. In this > c

Re: sorting a list numbers stored as strings

2007-09-24 Thread Carsten Haese
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote: > On 9/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > hi, > > > > I have the following list - > > > > ["1", "11", "2", "22"] > > > > how do I sort it like this - > > > > ["1", "2", "11", "22"] > > > > Hi, > > >>> l = ["1", "11", "2",

Re: sorting a list numbers stored as strings

2007-09-24 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > hi, > > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] source = ["1", "11", "2", "22"] result = [t[1] for t in sorted((int(item), item) for item in source)] print result -- http://mail.python.or

Re: sorting a list numbers stored as strings

2007-09-24 Thread Amit Khemka
On 9/24/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > hi, > > I have the following list - > > ["1", "11", "2", "22"] > > how do I sort it like this - > > ["1", "2", "11", "22"] > Hi, >>> l = ["1", "11", "2", "22"] >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y))) # provide your own comp