Re: Replacing cmp with key for sorting

2008-11-03 Thread bearophileHUGS
George Sakkis: > but I guess there's not much more room for improvement. That's nonsense, Python is a high level language, so there's nearly always room for improvement (even in programs written in assembly you can generally find faster solutions). If speed is what you look for, and your strings a

Re: Replacing cmp with key for sorting

2008-11-03 Thread George Sakkis
On Nov 3, 1:51 pm, [EMAIL PROTECTED] wrote: > Arnaud Delobelle: > > > Here's another idea, probably more practical: > > >>> sorted(s, key=lambda x: tuple(256-ord(l) for l in x), reverse=True) > > Nice. > A variant that probably works with unicode strings too: > > print sorted(s, key=lambda x: [-or

Re: Replacing cmp with key for sorting

2008-11-03 Thread bearophileHUGS
Arnaud Delobelle: > It's funny how the obvious escapes me so often. In this case it's a well known cognitive effect: the mind of humans clings to first good/working solution, not allowing its final tuning. For that you may need to think about something else for a short time, and then look at your

Re: Replacing cmp with key for sorting

2008-11-03 Thread Arnaud Delobelle
[EMAIL PROTECTED] writes: > Arnaud Delobelle: >> Here's another idea, probably more practical: >> >>> sorted(s, key=lambda x: tuple(256-ord(l) for l in x), reverse=True) > > Nice. > A variant that probably works with unicode strings too: > > print sorted(s, key=lambda x: [-ord(l) for l in x], reve

Re: Replacing cmp with key for sorting

2008-11-03 Thread bearophileHUGS
Arnaud Delobelle: > Here's another idea, probably more practical: > >>> sorted(s, key=lambda x: tuple(256-ord(l) for l in x), reverse=True) Nice. A variant that probably works with unicode strings too: print sorted(s, key=lambda x: [-ord(l) for l in x], reverse=True) Bye, bearophile -- http://ma

Re: Replacing cmp with key for sorting

2008-11-03 Thread bearophileHUGS
Alan G Isaac: > Probably not what you had in mind ... > ... > >>> maxlen = max(len(si) for si in s) >      >>> def k(si): return si+'z'*(maxlen-len(si)) This looks a little better: assert isinstance(s, str) sorted(s, key=lambda p: p.ljust(maxlen, "\255")) If the string is an unicode that ma

Re: Replacing cmp with key for sorting

2008-11-03 Thread Arnaud Delobelle
George Sakkis <[EMAIL PROTECTED]> writes: > I want to sort sequences of strings lexicographically but those with > longer prefix should come earlier, e.g. for s = ['a', 'bc', 'bd', > 'bcb', 'ba', 'ab'], the sorted sequence is ['ab', 'a', 'ba', 'bcb', > 'bc', 'bd']. Currently I do it with: > > s.so

Re: Replacing cmp with key for sorting

2008-11-03 Thread Alan G Isaac
George Sakkis wrote: s.sort(cmp=lambda x,y: 0 if x==y else -1 if x.startswith(y) else +1 if y.startswith(x) else cmp(x,y)) Probably not what you had in mind ... >>> s ['a', 'b

Re: Replacing cmp with key for sorting

2008-11-03 Thread bearophileHUGS
On Nov 3, 6:49 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > I want to sort sequences of strings lexicographically but those with > longer prefix should come earlier, e.g. for s = ['a', 'bc', 'bd', > 'bcb', 'ba', 'ab'], the sorted sequence is ['ab', 'a', 'ba', 'bcb', > 'bc', 'bd']. Currently I do