2016-04-11 7 views
0

文字列を示すPythonリストがあります。パターンが存在する場合に検索する

print ignore_list 
['name', 'wait', 'delete'] 

私は他の文字列を持っているフォーマット以下は次のとおりです:

My Name is xxxx 
OR 
Hello 
OR 
This is to be deleted 
etc 

は今、私はすべて除外する必要が他の文字列がリストに記載された任意の単語を含まれている場合は、さらなる処理のために、他の文字列を除外ignore_listで述べたようにstrが埋め込まれた文字列。私のコードは以下のようなものです:

if string_val and not string_val in ignore_list: 
print string_val 

しかし、上のコードでは、実際のようないくつかのことを比較するようだ:文字列値のいずれかのリストは、文字列に埋め込まれている場合、私はつまり、同じことを達成するにはどうすればよい

if "This is to be deleted" and "This is to be deleted" in 
['name', 'wait', 'delete']: 
print string_val ==> prints "This is to be deleted" as the same is not present in the list 

他の処理は無視する必要がありますか?

答えて

0

あなたは無視リスト上の単語ごとにメンバーシップをチェックするジェネレータ式でallを組み合わせることができます

if all(x not in s for x in ignore_list): 
    print s 
関連する問題