1

我有一个文本文件,每行都有一个句子。我有一个单词表。我只想从列表中获取至少包含一个单词的句子。有没有一种pythonic方法可以做到这一点?

4

3 回答 3

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

Here f would be your file object, for example you could replace it with open('file.txt') if file.txt was the name of your file and it was located in the same directory as the script.

于 2013-01-08T23:44:55.983 回答
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)
于 2013-01-08T23:54:49.490 回答
1

this will give you a start:

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
于 2013-01-08T23:44:41.970 回答