の回答/コメントを統合するには、別の回答のパフォーマンスに簡単なテストを行うことを願って...
>>> def timeTest(s, f):
... t1 = time.clock()
... for x in xrange(s):
... f(['ABC', 'DEF', 'GHI'], 'PQRABCD')
... f(['ABC', 'DEF', 'GHI'], 'PQRACDEF')
... f(['ABC', 'DEF', 'GHI'], 'PGHIQRCD')
... t2 = time.clock()
... print t2 - t1
...
>>>
>>> def func1(shortStrList, longStr):
... return shortStrList[[x in longStr for x in shortStrList].index(True)]
...
>>> timeTest(10000000, func1)
18.4710161502
>>>
>>> def func2(shortStrList, longStr):
... return next(s for s in shortStrList if s in longStr)
...
>>> timeTest(10000000, func2)
26.1494262581
>>>
>>> def func3(shortStrList, longStr):
... filter(lambda x: x in longStr, shortStrList)[0]
...
>>> timeTest(10000000, func3)
26.1221138429
>>>
>>> def func4(shortStrList, longStr):
... for s in shortStrList:
... if s in longStr: return s
...
>>> timeTest(10000000, func4)
8.78067844999
>>>
>>> def func5(shortStrList, longStr):
... return [string for string in shortStrList if string in longStr][0]
...
>>> timeTest(10000000, func5)
12.549210555
>>>
は、ループをやっように思えます(func4)としてEkeyme Moが最速です。 (これは1つのライナーとして書き直すことができるかどうかはわかりません)
短い文字列リストの長さが異なる場合、異なる方法が好まれるかもしれません。 シンプルループは最も速く実行されますが、next()はリストが長いときにリスト内包より速く実行します。
>>> def timeTest(s, f):
... sl = ['ABC'] + ['ZXYZ']*50 + ['DEF'] + ['RQDSF']*50 + ['GHI']
... t1 = time.clock()
... for x in xrange(s):
... f(sl, 'PQRABCD')
... f(sl, 'PQRACDEF')
... f(sl, 'PGHIQRCD')
... t2 = time.clock()
... print t2 - t1
...
>>> def func1(shortStrList, longStr):
... return shortStrList[[x in longStr for x in shortStrList].index(True)]
...
>>> timeTest(100000, func1)
2.14106761862
>>>
>>> def func2(shortStrList, longStr):
... return next(s for s in shortStrList if s in longStr)
...
>>> timeTest(100000, func2)
0.867831158122
>>>
>>> def func3(shortStrList, longStr):
... filter(lambda x: x in longStr, shortStrList)[0]
...
>>> timeTest(100000, func3)
3.19491244615
>>>
>>> def func4(shortStrList, longStr):
... for s in shortStrList:
... if s in longStr: return s
...
>>> timeTest(100000, func4)
0.629572839949
>>>
>>> def func5(shortStrList, longStr):
... return [string for string in shortStrList if string in longStr][0]
...
>>> timeTest(100000, func5)
1.31148152449
>>>
あなたが '、 'FUNC([ 'ABC'、 'DEF'、 'GHI']の出力を何を期待しABCDEF ') 'は? – DeepSpace
私には十分にピジョンがあるようです。 – Eduard
@DeepSpace「ABC」を返すのが好きです。すなわち、最初のマッチ – Porz