2016-08-30 3 views
-1

pythonで以下のケース1をどのように一致させるか...文の中のすべての単語をリストと一致させたい。Pythonを使用して単一の文を持つリスト内のすべての単語を一致させるには

l1=['there is a list of contents available in the fields'] 
>>> 'there' in l1 
False 
>>> 'there is a list of contents available in the fields' in l1 
True 
+2

あなたのリストには1つの要素しか含まれていませんが、そこに文字列を持たない点は何ですか?とにかく、l1 [0] 'の' there 'はTrueを返します。 – Efferalgan

+0

ウェブスクレイピングを実行しています..同様の方法が必要です.. –

答えて

1

簡単な方法

l1=['there is a list of contents available in the fields'] 
>>> 'there' in l1[0] 
True 

良い方法は、リストのすべての要素を反復することがウィル。

l1=['there is a list of contents available in the fields'] 
print(bool([i for i in l1 if 'there' in i])) 
0

あなただけのリスト内の文字列のいずれかが関係なく、それはあなたがこれを行うことができますされている文字列の単語が含まれていないかどうかを知りたい場合は:今、あなたがものをフィルタリングする場合は

if any('there' in element for element in li): 
    pass 

li = filter(lambda x: 'there' in x, li) 

やPython 3:

li = list(filter(lambda x: 'there' in x, li)) 
いるあなたは、単にできる文字列にマッチします
関連する問題