【发布时间】:2021-08-13 04:25:36
【问题描述】:
我正在尝试返回列表中包含“任何”单词的所有句子,但结果仅返回列表中第二个单词的句子。在下面的示例中,我想提取包含通货膨胀和商品的句子,而不仅仅是商品。任何帮助将不胜感激。
text = 'inflation is very high. commodity prices are rising a lot. this is an extra sentence'
words = ['inflation', 'commodity']
for word in words:
[words.casefold() for words in words] #to ignore cases in text
def extract_word(text):
return [sentence for sentence in text.split('.') if word in sentence]
extract_word(text)
[' commodity prices are rising a lot']
【问题讨论】:
-
请阅读以下内容并提供完整的minimal reproducible example
标签: python