获取python模糊匹配的索引

2024-09-27 22:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Pythonfuzzywuzzy在句子列表中查找匹配项:

def getMatches(needle):
     return process.extract(needle, bookSentences, scorer=fuzz.token_sort_ratio, limit=3)

我正试着打印出匹配的句子:

^{pr2}$

很遗憾,脚本在原始列表中找不到匹配项:

ValueError: (u'Thus, in addition to the twin purposes mentioned above, this book is written for at least two groups: 1.', 59) is not in list

有没有更好的方法可以在原始列表中找到匹配项的索引?能给我一点吗?在readme里似乎没有关于它的任何东西。在

如何获得fuzzywuzzy返回的匹配项的原始列表中的索引?在


Tags: intoken列表returnisdefextractprocess
1条回答
网友
1楼 · 发布于 2024-09-27 22:38:56

我觉得有点傻。fuzzywuzzy返回一个包含分数的元组,而不仅仅是匹配项。解决方案:

for match in matches:
     matchIndex = bookSentences.index(match[0])
     sentenceIndices = range(matchIndex-2,matchIndex+2)
     for index in sentenceIndices:
         print bookSentences[index],
     print '\n\n'

相关问题 更多 >

    热门问题