473,500 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replacing cmp with key for sorting

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.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))

Can this be done with an equivalent key function instead of cmp ?

George
Nov 3 '08 #1
9 1823
On Nov 3, 6:49*pm, George Sakkis <george.sak...@gmail.comwrote:
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.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))

Can this be done with an equivalent key function instead of cmp ?

George
Your input and output:

s = ['a', 'bc', 'bd', 'bcb', 'ba', 'ab']
r = ['ab', 'a', 'ba', 'bcb', 'bc', 'bd']

To me your lambda looks like an abuse of the inline if expression. So
I suggest to replace it with a true function, that is more readable:

def mycmp(x, y):
if x == y:
return 0
elif x.startswith(y):
return -1
elif y.startswith(x):
return +1
else:
return cmp(x, y)

print sorted(s, cmp=mycmp)

It's a peculiar cmp function, I'm thinking still in what situations it
can be useful.

To use the key argument given a cmp function I use the simple code
written by Hettinger:

def cmp2key(mycmp):
"Converts a cmp= function into a key= function"
class K:
def __init__(self, obj, *args):
self.obj = obj
def __cmp__(self, other):
return mycmp(self.obj, other.obj)
return K
print sorted(s, key=cmp2key(mycmp))

Now I'll look for simpler solutions...

Bye,
bearophile
Nov 3 '08 #2
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', 'bc', 'bd', 'bcb', 'ba', 'ab']
>>maxlen = max(len(si) for si in s)
def k(si): return si+'z'*(maxlen-len(si))
...
>>sorted(s,key=k)
['ab', 'a', 'ba', 'bcb', 'bc', 'bd']

Cheers,
Alan Isaac
Nov 3 '08 #3
George Sakkis <ge***********@gmail.comwrites:
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.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))

Can this be done with an equivalent key function instead of cmp ?
Here's an idea:
>>sorted(s, key=lambda x: x+'z'*(3-len(s)))
['ab', 'a', 'ba', 'bcb', 'bc', 'bd']

The 3 above is the length of the longest string in the list

Here's another idea, probably more practical:
>>sorted(s, key=lambda x: tuple(256-ord(l) for l in x), reverse=True)
['ab', 'a', 'ba', 'bcb', 'bc', 'bd']

HTH

--
Arnaud

Nov 3 '08 #4
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 may not work anymore.
I don't know if there are better solutions.

Bye,
bearophile
Nov 3 '08 #5
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
Nov 3 '08 #6
be************@lycos.com 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], reverse=True)
Of course that's better! (although mine will work with unicode if yours
does). It's funny how the obvious escapes me so often. Still I think
the idea of the 'double reverse' (one letterwise, the other listwise)
was quite good.

--
Arnaud
Nov 3 '08 #7
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 solution with a little "fresher" mind.

This (ugly) translation into D + my functional-style libs shows why
Python syntax is a good idea:

import d.all;
void main() {
auto txt = "a bc bd bcb ba ab".split();
putr( sorted(txt, (string s){ return map((char c){return -
cast(int)c;}, s);} ).reverse );
}

Long Live To Python! :-)

Bye,
bearophile
Nov 3 '08 #8
On Nov 3, 1:51*pm, bearophileH...@lycos.com 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: [-ord(l) for l in x], reverse=True)

Bye,
bearophile
Awesome! I tested it on a sample list of ~61K words [1] and it's
almost 40% faster, from ~1.05s dropped to ~0.62s. That's still >15
times slower than the default sorting (0.04s) but I guess there's not
much more room for improvement.

George

[1] http://www.cs.pitt.edu/~kirk/cs1501/...ggle/5desk.txt
Nov 3 '08 #9
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 are ASCII then this is
much faster:

tab = "".join(map(chr, xrange(256)))[::-1]
s.sort(key=lambda x: x.translate(tab), reverse=True)

Bye,
bearophile
Nov 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2515
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
7
3231
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
16
2416
by: StenKoll | last post by:
Help needed in order to create a register of stocks in a company. In accordance with local laws I need to give each individual share a number. I have accomplished this by establishing three tables...
19
25423
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
2748
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
4
3081
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
7
4793
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
1
7164
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
4901
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.