gpt4 book ai didi

python - 模糊匹配排名

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:28 25 4
gpt4 key购买 nike

我模糊匹配了一个电影标题列表,并将它们与匹配值一起编译到每个比较的另一个列表中:

>>> fuzzy_matches
[(['White Warrior (Alpha Video)'], ['White Warrior (Alpha Video)'], 100), (['White Warrior (Alpha Video)'], ['White Warrior (Digiview Entertainment)'], 63), (['White Warrior (Alpha Video)'], ['White Warrior (Platinum)'], 78), (['White Warrior (Alpha Video)'], ['White Warrior (Platinum) / David And Goliath'], 63), (['White Warrior (Alpha Video)'], ['White Warrior (Platinum) / Duel Of Champions'], 61)]...etc

我想将每个标题的匹配值相加,以便得到如下输出:

>>>([White Warrior (Alpha Video)], 248),
['White Warrior 2 (Digiview Entertainment)'], 390),
etc...

我尝试了几种使用切片的实现,但它很丑陋。

(不是我的确切代码,但这就是丑陋之处):

for x in range(len(fuzzed)):
for y in fuzzed(len(fuzzed)):

big_dict[fuzzy_matches[55][0][0]]=fuzzy_matches[55][2] + fuzzy_matches[56][3]...

什么是更有效的方法来实现这一目标?

最佳答案

你可以使用字典来存储你想要的结果,然后最后如果你想要一个元组列表,你可以使用 dict.items() (Python 3.x)明白了。

示例 -

>>> fuzzy_matches = [(['White Warrior (Alpha Video)'], ['White Warrior (Alpha Video)'], 100), (['White Warrior (Alpha Video)'], ['White Warrior (Digiview Entertainment)'], 63), (['White Warrior (Alpha Video)'], ['White Warrior (Platinum)'], 78), (['White Warrior (Alpha Video)'], ['White Warrior (Platinum) / David And Goliath'], 63), (['White Warrior (Alpha Video)'], ['White Warrior (Platinum) / Du
el Of Champions'], 61)]
>>>
>>> fuzzy_dict = {}
>>> for i in fuzzy_matches:
... if i[0][0] not in fuzzy_dict:
... fuzzy_dict[i[0][0]] = 0
... fuzzy_dict[i[0][0]] += i[2]
...
>>> fuzzy_dict
{'White Warrior (Alpha Video)': 365}
>>> list(fuzzy_dict.items())
[('White Warrior (Alpha Video)', 365)]

如果您使用的是 Python 2.x,则末尾不需要 list(...)

关于python - 模糊匹配排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31396052/

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