os.path.commonprefix inadequacies

Rich Harkins rharkins at thinkronize.com
Wed Aug 1 11:00:29 EDT 2001


Oops!  This function also works with strings (I think any sequence type
would do)...

commonprefix("abc","ab","abe") => "ab"

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Rich Harkins
> Sent: Wednesday, August 01, 2001 10:37 AM
> To: Python-List
> Subject: RE: os.path.commonprefix inadequacies
>
>
> 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
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list