如何判断是否在较大的字符串中找到字符串列表?

时间:2015-10-27 23:21:26

标签: python python-2.7 python-3.x

我有一个单词列表(即):

['bad', 'good', 'smart']

我需要找到文件中哪些行包含 all 该行中的那些单词。我试过了:

for line in file_text: 
    if [w for w in list] in line:
        print(line) 

但是我收到以下错误:

TypeError: 'in <string>' requires string as left operand, not list

这是一种简单的pythonic方法吗?

2 个答案:

答案 0 :(得分:3)

我认为你想使用all(或any):

>>> lst = ["the", "fox", "brown"]
>>> sentence = "the quick brown fox jumps over the lazy dog"
>>> all(word in sentence for word in lst)
True

答案 1 :(得分:0)

<强>单行

for line in file_text:
    [line for w in list if w in line and w == 'smart']