【发布时间】:2020-10-10 20:03:20
【问题描述】:
我遇到了与此链接中讨论的相同的问题 Python extracting sentence containing 2 words
但不同之处在于我只需要在定义大小的窗口搜索中提取包含这两个单词的句子。例如:
sentences = [ 'There was peace and happiness','hello every one',' How to Find Inner Peace ,love and Happiness ','Inner peace is closely related to happiness']
search_words= ['peace','happiness']
windows_size = 3 #search only the three words after the 1est word 'peace'
#output must be :
output= ['There was peace and happiness',' How to Find Inner Peace love and Happiness ']
【问题讨论】:
-
解释清楚一点。您的输出不符合条件。
-
我没有看到输出与条件不匹配的地方,对于输出中的第一句话:'幸福在窗户的第二个位置,所以它满足条件,并且在输出的第二句:'幸福在第三位,它满足条件,所以它是真的。输入句子中的最后一句话在哪里:“幸福”这个词在第 5 位,所以我们不接受它。窗口计数从位置 i+1 开始。我希望它更清楚?
标签: python regex string list find