gpt4 book ai didi

python - 如何根据 Python 中的部分匹配从文本中删除子字符串?

转载 作者:行者123 更新时间:2023-11-28 22:12:12 24 4
gpt4 key购买 nike

我有一个很长的文本 block ,其中包含我想根据部分匹配 (90%) 删除的子文本。

string = "Adam is a boy who lives in Michigan.  
He loves to eat apples and oranges.
He also enjoys playing with his dog and cat.
Adam is a happy boy."

substring = "He loves to apple oranges"

我想回去

"Adam is a boy who lives in Michigan.  
He also enjoys playing with his dog and cat.
Adam is a happy boy."

单词“eat”和“and”没有出现在子字符串中,但我想删除整个句子“He loves to eat apples and oranges”。我不太确定该怎么做。谢谢!

最佳答案

您可以使用 difflib.SequenceMatcher :

from difflib import SequenceMatcher
'\n'.join(s for s in string.splitlines() if SequenceMatcher(' '.__eq__, s, substring).ratio() < 0.6)

返回:

Adam is a boy who lives in Michigan.
He also enjoys playing with his dog and cat.
Adam is a happy boy.

演示:https://ideone.com/twDu1r

关于python - 如何根据 Python 中的部分匹配从文本中删除子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55154696/

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