os.path.commonprefix inadequacies
Rich Harkins
rharkins at thinkronize.com
Wed Aug 1 10:37:04 EDT 2001
Here's yet another way to do a common prefix, not sure about its performance
characteristics (assuming you have lists):
def commonprefix(*lists):
assert len(lists) > 1
l=zip(*lists)
for n in xrange(len(l)):
if filter(None,map(cmp,l[n][1:],l[n][0]*(len(l[n])-1))):
return lists[0][:n]
return lists[0]
# Some tests
print commonprefix(('a','b','c'),('a','b','x'),('a','b','c','y'))
# Returns ('a','b')
print commonprefix(('a','b'),('x'))
# Returns ()
print commonprefix(('x','y','z'),('x','y','z'))
# Returns ('x','y','z')
Hope this helps!
Rich
More information about the Python-list
mailing list