python - 如何在两个列表中找到紧密匹配的唯一元素? (这里使用距离函数)

标签 python list

我正在尝试创建一个名称匹配器来比较'JOHN LEWIS''JOHN SMITH LEWIS'。他们显然是同一个人,我想创建一个函数,当您输入这些名称时,它会将其转换为列表,然后为您提供匹配的名称。

问题是我的循环返回 'LEWIS''LEWIS' 匹配,而 'SMITH' 匹配'LEWIS' 因为它的顺序。

from pyjarowinkler import distance

entered_name = 'JOHN LEWIS'.split(' ')  # equals ['JOHN','LEWIS']
system_name = 'JOHN SMITH LEWIS'.split(' ')  # equals ['JOHN','SMITH','LEWIS']

ratio = []

for i in entered_name:
    maximum = 0 
    for j in system_name:
        score = distance.get_jaro_distance(i, j, winkler=True, 
                                           scaling=0.1)
        while score > maximum:
            maximum = score
            new = (i, j, maximum)
            system_name.remove(i) 
            #removes that name from the original list
    ratio.append(new)

将返回类似以下内容的内容:[('JOHN', 'JOHN', 1.0), ('LEWIS', 'SMITH', 0.47)]

而不是:[('JOHN', 'JOHN', 1.0), ('LEWIS', 'LEWIS', 1.0)] <-这就是我想要的。

此外,如果您尝试使用 'ALLY A ARM''ALLY ARIANA ARMANI' ,它会匹配 'ALLY' 两次,如果您不执行 remove(i) 行。这就是为什么我只想要独特的匹配!

我只是不断收到错误或我不需要的答案。

最佳答案

问题出在您的 system_name.remove(i) 行上。首先,在迭代列表时修改列表通常不是一个好主意。这可能会导致意外的行为。在您的情况下,您的代码正在执行以下操作:

  1. 第一次匹配 'JOHN''JOHN'。没问题。
  2. system_name 中删除 'JOHN'。现在system_name = ['SMITH', 'LEWIS']
  3. 第二次,i = 'LEWIS'j = 'SMITH'score = .47 大于 0,所以你的检查分数>最大通过了
  4. 我们设置最大值 = 分数
  5. 我们设置new = ('LEWIS', 'SMITH', 0.47)
  6. 我们从 system_name 中删除 'LEWIS'。现在system_name = ['SMITH']。呃哦...

下面简单重写,使用 if 而不是 while 循环,因为 while 循环完全没有必要:

for i in entered_name:
    maximum = 0 
    for j in system_name:
        score = distance.get_jaro_distance(i, j, winkler=True, 
                                           scaling=0.1)
        if score > maximum:
            maximum = score
            new = (i, j, maximum)
    system_name.remove(new[1])  # want to remove 'SMITH' in the example, not 'LEWIS' 
    ratio.append(new)

我所做的就是将 system_name.remove() 调用移到 system_name 的循环之外,并将 i 替换为 j (使用 new[1] 因为我在 j 循环之外)。

关于python - 如何在两个列表中找到紧密匹配的唯一元素? (这里使用距离函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030798/

相关文章:

python - 如何从列名称为 "crashloc"的簇中获取值,其中 "crashtype"= ="Serious Suspected Injury"

python - 如何让这个函数从Python中的数组行返回组合对?

python - 使用 Scapy 制作 DTLS ClientHello 数据包

python - 从重复出现的单词右侧的 n 个元素形成新列表

python - 如何在Python列表中添加/删除多个对象实例?

python - 在 Python 中将列表写入和读取文本文件 : Is there a more efficient way?

java - 如果我经常使用 contains 方法,是否有比 HashMap 更好的 DS 来存储项目列表?

r - 如何省略 do.call 产生的大量代码?

python - 为什么 Python 的 sorted() 方法不会反转字典中具有相同值的键的顺序?

python - 在 xarray dataarray 中重命名 __xarray_dataarray_variable__