Kawu Kawu - 1 year ago 64
Python Question

Python: Determine prefix from a set of (similar) strings

I have a set of strings, e.g.


I simply want to find the longest common portion of these strings, here the prefix. In the above the result should be


The strings


should result in the prefix


Is there a relatively painless way in Python to determine the prefix (without having to iterate over each character manually)?

PS: I'm using Python 2.6.3.

Answer Source

Never rewrite what is provided to you: os.path.commonprefix does exactly this:

Return the longest path prefix (taken character-by-character) that is a prefix of all paths in list. If list is empty, return the empty string (''). Note that this may return invalid paths because it works a character at a time.

For comparison to the other answers, here's the code:

# Return the longest prefix of all list elements.
def commonprefix(m):
    "Given a list of pathnames, returns the longest common leading component"
    if not m: return ''
    s1 = min(m)
    s2 = max(m)
    for i, c in enumerate(s1):
        if c != s2[i]:
            return s1[:i]
    return s1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download