gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:08 28 4
gpt4 key购买 nike

我编写了一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com