python - 比较多个 Python 列表并合并 Levenshtein 相似性

标签 python list function compare levenshtein-distance

我编写了一个 Python 函数,它接受两个列表,使用 Levenshtein 比较它们并将足够相似的单词合并到一个名为“merged”的列表中。

我如何为超过 6 个列表执行此操作?确保将每个列表与其他 5 个列表进行比较等等?

first_list = ["Mouse", "Cat", "Dog", "Gremlinge", "Horse"]
second_list = ["Mouse", "Cat", "Hors", "Dog", "Gremling"]
third_list = ["Mouse", "Cat", "Horrs", "Dog", "Greemling"]
fourth_list = ["Mouse", "Cate", "Dog", "Gremlinge", "Horse"]
fifth_list = ["Mose", "Cat", "Hors", "Dog", "Gremling"]
sixth_list = ["Mouse", "Cat", "Horser", "Doeg", "Gremling"]

def lev_merging(a, b): # function to compare 2 lists
  merged = [] # Empty list to add the matching words
  for first in a:
    for second in b:
      if levenshtein(first, second) < 2:
        merged.append(set([first,second]))
  return merged

print (lev_merging(first_list,second_list))

Working www.repl.it fiddle of code.

最佳答案

我们将有一个字符串列表列表

list_of_lists = [["Mouse", "Cat", "Dog", "Gremlinge", "Horse"],
                  ["Mouse", "Cat", "Hors", "Dog", "Gremling"],
                  ["Mouse", "Cat", "Horrs", "Dog", "Greemling"],
                  ["Mouse", "Cate", "Dog", "Gremlinge", "Horse"],
                  ["Mose", "Cat", "Hors", "Dog", "Gremling"],
                  ["Mouse", "Cat", "Horser", "Doeg", "Gremling"]]

然后我们将遍历此列表,跟踪我们“在其中”的列表的索引,并将此列表与它之后的所有列表进行比较。

def merging(list_of_lists):
    merged = []
    for i, a in enumerate(list_of_lists):
        for b in list_of_lists[i+1:]:
            for first in a:
                for second in b:
                    if lev(first, second) < 2:
                        merged.append((first, second))
    return merged

编辑:下面的代码将成对的列表传递给一个函数,并将它们分成组。然后我们将把这些组中的每一个处理成集合,以删除重复项。

target_num_words = 6
target_num_words

def merging(list_of_lists):
    groups = []
    for i, a in enumerate(list_of_lists):
        for b in list_of_lists[i+1:]:
            if number_of_matches(a, b) >= target_num_words:
                for g in groups:
                    if a in g or b in g:
                        g.append(a if b in g else b)
                        break
                else:
                    groups.append([a, b])
    merged = []
    for g in groups:
        if len(g) >= target_num_lists:
            merged.append({x for l in g for x in l})
    return merged

number_of_matches 基本上是您的 Levenshtein 代码,只是它只返回两个列表之间匹配单词的数量。即使这不是您想要的,这也应该让您了解如何实现目标。

关于python - 比较多个 Python 列表并合并 Levenshtein 相似性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42631240/

相关文章:

java - 如何将 Map<T,List<L>> 映射中的值添加到 List<L>?

python - 对于列表的每个成员,将其写入单独的文件 python

c++ - C++ 中的模板函数?也许不是正确的术语

Python 的 all() 没有给出预期结果,检查小数字列表是否小于某个值

python - 如何从python向node.js发送post请求以利用geckoboard?

python - 如何动态地将列/值添加到 pyspark 数据框中的映射类型

python - python 列表指向行为

c++ - 在 C++ 中中断函数

python - 沿特定维度从 ndarray 中减去矩阵而不重新整形

python - scrapy 中的项目 vs 项目加载器