gpt4 book ai didi

Python提取包含单词的句子

转载 作者:太空狗 更新时间:2023-10-29 20:32:20 25 4
gpt4 key购买 nike

我正在尝试从文本中提取所有包含指定单词的句子。

txt="I like to eat apple. Me too. Let's go buy some apples."
txt = "." + txt
re.findall(r"\."+".+"+"apple"+".+"+"\.", txt)

但它正在返回我:

[".I like to eat apple. Me too. Let's go buy some apples."]

代替:

[".I like to eat apple., "Let's go buy some apples."]

有什么帮助吗?

最佳答案

不需要正则表达式:

>>> txt = "I like to eat apple. Me too. Let's go buy some apples."
>>> [sentence + '.' for sentence in txt.split('.') if 'apple' in sentence]
['I like to eat apple.', " Let's go buy some apples."]

关于Python提取包含单词的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16032832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com