擅长:python、mysql、java
<p>永远不要重写提供给您的内容:<a href="https://docs.python.org/3/library/os.path.html#os.path.commonprefix">^{<cd1>}</a>这样做正是:</p>
<blockquote>
<p>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 (<code>''</code>). Note that this may return
invalid paths because it works a character at a time.</p>
</blockquote>
<p>为了与其他答案进行比较,下面是代码:</p>
<pre><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
</code></pre>