是否有Python库或方法来确定最佳匹配

2024-06-06 18:52:00 发布

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

我想看看如何从5条记录中选出“最有可能”的值。我认为fuzzywuzzy包可能可以工作,但我想知道即使没有提供搜索字符串,这个或另一个包是否也可以工作

我尝试了正则表达式,但认为它根本不起作用,然后遇到了以下fuzzywuzzy代码: https://www.datacamp.com/community/tutorials/fuzzy-string-python

from fuzzywuzzy import process

str2Match = "apple inc"
strOptions = ["Apple Inc.","apple park","apple incorporated","iphone"]
Ratios = process.extract(str2Match,strOptions)

# You can also select the string with the highest matching percentage
highest = process.extractOne(str2Match,strOptions)

结果是:

>> print (Ratios)
[('Apple Inc.', 100), ('apple incorporated', 90), ('apple park', 67), ('iphone', 30)]

>> print(highest)
('Apple Inc.', 100)

上面的结果是有意义的,因为有一个提供的搜索字符串str2Match,但我想知道,代码是否有可能自动生成最佳(最高)值(例如,当它意识到'Apple Inc''apple incorporated'足够相似时,自动生成一些值)?谢谢


Tags: 字符串代码parkapplestringprocessinciphone