模糊匹配值列表python的列表
努力以pythonic的方式做这件事。我有一个列表,我们可以调用名称
[('Jimmy', 'Smith'), ('James', 'Wilson'), ('Hugh' "Laurie')]
,然后我有两个变量
First_name = 'Jimm'
Last_name = 'Smitn'
,我想通过此列表列表,一个和姓氏的列表,以模糊为匹配这些值,并返回最接近指定的first_name的列表和last_name
Struggling with how to do this in a pythonic way. I have a list of list which we can call names
[('Jimmy', 'Smith'), ('James', 'Wilson'), ('Hugh' "Laurie')]
And then I have a two variables
First_name = 'Jimm'
Last_name = 'Smitn'
I want to iterate through this list of list, of first and last names to fuzzy match these values and return the list that is the closest to the specified First_name and Last_name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以实现模糊匹配,获得最佳匹配比(使用” () )由
difflib.Sequecematcher()
。要实现此目的,我们应该通过
lambda
作为
,但是如果性能很重要,您也应该尝试键
参数,将返回匹配比率。在我的示例中,我将使用 )sequencematcher.quick_ratio()
和sequencematcher.real_quick_ratio()
。You can implement fuzzy matching obtaining best match ratio (using
max()
) returned bydifflib.SequenceMatcher()
.To implement this we should pass
lambda
askey
argument which will return match ratio. In my example I'd useSequenceMatcher.ratio()
, but if performance is important you should also try withSequenceMatcher.quick_ratio()
andSequenceMatcher.real_quick_ratio()
.另一个可能的路径是使用集合交叉点。
Another possible path would be to use set intersections.