Re: sort order for strings of digits

2012-11-01 Thread Steven D'Aprano
On Thu, 01 Nov 2012 11:53:06 +1100, Chris Angelico wrote: > On Thu, Nov 1, 2012 at 10:44 AM, Steven D'Aprano > wrote: >> On the contrary. If you are using cmp with sort, your sorts are slow, >> and you should upgrade to using a key function as soon as possible. >> >> > But cmp_to_key doesn't actu

Re: sort order for strings of digits

2012-11-01 Thread wxjmfauth
Le mercredi 31 octobre 2012 16:17:19 UTC+1, djc a écrit : > I learn lots of useful things from the list, some not always welcome. No > > sooner had I found a solution to a minor inconvenience in my code, than > > a recent thread here drew my attention to the fact that it will not work > > for

Re: sort order for strings of digits

2012-10-31 Thread Arnaud Delobelle
On 31 October 2012 23:09, Steven D'Aprano wrote: > The trick is to take each string and split it into a leading number and a > trailing alphanumeric string. Either part may be "empty". Here's a pure > Python solution: > > from sys import maxsize # use maxint in Python 2 > def split(s): > for

Re: sort order for strings of digits

2012-10-31 Thread Chris Angelico
On Thu, Nov 1, 2012 at 10:44 AM, Steven D'Aprano wrote: > On the contrary. If you are using cmp with sort, your sorts are slow, and > you should upgrade to using a key function as soon as possible. > But cmp_to_key doesn't actually improve anything. So I'm not sure how Py3 has achieved anything;

Re: sort order for strings of digits

2012-10-31 Thread Mark Lawrence
On 31/10/2012 22:24, Ian Kelly wrote: On Wed, Oct 31, 2012 at 3:33 PM, Mark Lawrence wrote: Nope. I'm busy porting my own code from 2.7 to 3.3 and cmp seems to be very dead. This doesn't help either. c:\Users\Mark\Cash\Python>**2to3.py Traceback (most recent call last): File "C:\Python33

Re: sort order for strings of digits

2012-10-31 Thread DJC
On 31/10/12 23:09, Steven D'Aprano wrote: On Wed, 31 Oct 2012 15:17:14 +, djc wrote: The best I can think of is to split the input sequence into two lists, sort each and then join them. According to your example code, you don't have to split the input because you already have two lists, o

Re: sort order for strings of digits

2012-10-31 Thread Steven D'Aprano
On Wed, 31 Oct 2012 19:05:17 -0400, Dennis Lee Bieber wrote: >> The cmp builtin is also gone. If you need it, the suggested >> replacement for "cmp(a, b)" is "(b < a) - (a < b)". >> > OUCH... Just another reason for my to hang onto the 2.x series as > long as possible On the contrary. If

Re: sort order for strings of digits

2012-10-31 Thread Steven D'Aprano
On Wed, 31 Oct 2012 15:17:14 +, djc wrote: > The best I can think of is to split the input sequence into two lists, > sort each and then join them. According to your example code, you don't have to split the input because you already have two lists, one filled with numbers and one filled wit

Re: sort order for strings of digits

2012-10-31 Thread Ian Kelly
On Wed, Oct 31, 2012 at 3:33 PM, Mark Lawrence wrote: > Nope. I'm busy porting my own code from 2.7 to 3.3 and cmp seems to be > very dead. > > This doesn't help either. > > c:\Users\Mark\Cash\Python>**2to3.py > > Traceback (most recent call last): > File "C:\Python33\Tools\Scripts\**2to3.py",

Re: sort order for strings of digits

2012-10-31 Thread Mark Lawrence
On 31/10/2012 18:17, Dennis Lee Bieber wrote: Why -- I doubt Python 3.x .sort() and sorted() have removed the optional key and cmp keywords. Nope. I'm busy porting my own code from 2.7 to 3.3 and cmp seems to be very dead. This doesn't help either. c:\Users\Mark\Cash\Python>2to3.

Re: sort order for strings of digits

2012-10-31 Thread Ian Kelly
On Wed, Oct 31, 2012 at 9:17 AM, djc wrote: > The best I can think of is to split the input sequence into two lists, sort > each and then join them. In the example you have given they already seem to be split, so you could just do: sorted(n, key=int) + sorted(s) If that's not really the case, t

Re: sort order for strings of digits

2012-10-31 Thread Hans Mulder
On 31/10/12 16:17:14, djc wrote: > Python 3.2.3 (default, Oct 19 2012, 19:53:16) > sorted(n+s) > ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40', > 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] > sorted(int(x) if x.isdigit() else x for x in n+s) > Traceback (most recent