小编Her*_*ert的帖子

提取段落中与列表中单词相似的单词

我有以下字符串:

"The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"
Run Code Online (Sandbox Code Playgroud)

要提取的单词列表:

["town","teddy","chicken","boy went"]
Run Code Online (Sandbox Code Playgroud)

注意:town 和 teddy 在给定的句子中拼写错误。

我尝试了以下方法,但我得到了不属于答案的其他词:

import difflib

sent = "The boy went to twn and bought sausage and chicken. He then picked a tddy for his sister"

list1 = ["town","teddy","chicken","boy went"]

[difflib.get_close_matches(x.lower().strip(), sent.split()) for x in list1 ]
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

[['twn', 'to'], ['tddy'], ['chicken.', 'picked'], ['went']]
Run Code Online (Sandbox Code Playgroud)

代替:

'twn', 'tddy', 'chicken','boy went'
Run Code Online (Sandbox Code Playgroud)

python difflib python-3.x

2
推荐指数
1
解决办法
109
查看次数

标签 统计

difflib ×1

python ×1

python-3.x ×1