2013-09-27 99 views
0
strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz'] 
str = "-20.0dB" 

我需要得到True這裏,因爲它會檢查列表中的每個項目的列表中的每個項目 - strSpecialChars字符串str英寸如何找到一個字符串

+2

['any()'](http://docs.python.org/2/library/functions.html#any) – TerryA

回答

0
map(lambda s: s in "-20.0dB", strSpecialChars) 

您可能需要通過list轉換輸出才能看到它。

+0

謝謝。它的工作:) –

2

使用any() function測試每個值:

>>> strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz'] 
>>> yourstr = "-20.0dB" 
>>> any(s in yourstr for s in strSpecialChars) 
True 

,我改名stryourstr以避免掩蓋內置型。

any()只會提前傳遞給它的生成器表達式,直到返回True值;這意味着只有前3個選項會針對您的示例進行測試。

你可以在這裏使用str.endswith()

any(yourstr.endswith(s) for s in strSpecialChars) 

到比賽僅限制於那些年底與任何特殊字符。

+0

謝謝。有效 :) –

相關問題