检查字符串是否包含一组字符串中的任何项目?

时间:2013-01-08 23:41:48

标签: python

我有一个文本文件,每行都有一个句子。我有一个单词列表。我只想获得列表中至少包含一个单词的句子。是否有一种pythonic方式来做到这一点?

3 个答案:

答案 0 :(得分:4)

sentences = [line for line in f if any(word in line for word in word_list)]

此处f将是您的文件对象,例如,如果open('file.txt')是您的文件名称并且它与脚本位于同一目录中,则可以将其替换为file.txt

答案 1 :(得分:2)

使用set.intersection

with open('file') as f:
    [line for line in f if set(line.lower().split()).itersection(word_set)]

filter

filter(lambda x:word_set.intersection(set(x.lower().split())),f)

答案 2 :(得分:1)

这会给你一个开始:

words = ['a', 'and', 'foo']
infile = open('myfile.txt', 'r')
match_sentences = []

for line in infile.readlines():
    # check for words in this line
    # if match, append to match_sentences list