我正在做一些基于字符串编辑距离的实体匹配,结果是一个字典,其中包含键(查询字符串)和基于一些评分标准的值[相似字符串列表]。你知道吗
例如:
results = {
'ben' : ['benj', 'benjamin', 'benyamin'],
'benj': ['ben', 'beny', 'benjamin'],
'benjamin': ['benyamin'],
'benyamin': ['benjamin'],
'carl': ['karl'],
'karl': ['carl'],
}
每个值也有一个对应的字典项,它是键(例如“carl”和“karl”)。你知道吗
我需要把有共同价值的元素结合起来。选择一个值作为新键(比如说最长的字符串)。在上面的例子中,我希望得到:
results = {
'benjamin': ['ben', 'benj', 'benyamin', 'beny', 'benjamin', 'benyamin'],
'carl': ['carl','karl']
}
我尝试过使用键遍历字典,但是我不知道如何遍历和比较每个字典项及其值列表(或单个值)。你知道吗
编辑:尽管性能不是这里的问题,我还是冒昧地在jpp的答案和我的答案之间进行了一些测试。。。这是完整的剧本。我的脚本在17.79秒内完成测试,他的脚本在23.5秒内完成。你知道吗
如果我将导入从他的函数移到全局范围,它将给出。。。你知道吗
imcoins:13.4129249463秒
jpp:21.8191823393秒
这是一个使用
collections.defaultdict
和集合的解决方案。你知道吗所需的输出与您所拥有的非常相似,并且可以很容易地进行调整。你知道吗
在第二个循环中操纵
v
到w
的想法归功于@IMCoins。你知道吗解释
主要有3个步骤:
相关问题 更多 >
编程相关推荐