2016-10-28 1 views
0

内のすべての単語の最長共通文字列プレフィックスは、すべての値がリストにして開始すること最長の文字列(の長さ)を返す関数があります取得します4リスト

この場合:["flexible","flexile","flexion","flexor","ape"]、空の文字列または0を返します。

答えて

13
>>> import os 
>>> os.path.commonprefix(["flexible","flexile","flexion","flexor"]) 
'flex' 
+0

驚きのユースケースを、確かに! –

0

この任意の反復可能オブジェクトのために働く必要があります。

from itertools import takewhile 

def commonprefix(xs): 
    return map(lambda xs: xs[0],takewhile(lambda xs: len(set(xs)) == 1,zip(*xs))) 

print len(commonprefix(["flexible","flexile","flexion","flexor"])) # 4 
print len(commonprefix(["flexible","flexile","flexion","flexor","ape"])) # 0 
関連する問題