gpt4 book ai didi

python - 比较两个列表中常见项目的最快方法

转载 作者:行者123 更新时间:2023-12-04 01:30:23 25 4
gpt4 key购买 nike

我有两个这样的列表:

listt = [["a","abc","zzz","xxx","abc","abc"],["yyy","ggg","abc","cccc"]]

我有另一个这样的查询列表:

queryList = ["abc","cccc","abc","yyy"]

queryListlistt[0] 包含 2 个共同的 "abc"

queryList & listt[1] 包含 1 "abc", 1 "cccc" & 1 "yyy" 共同。

所以我想要这样的输出:

[2,3] #2 = Total common items between queryList & listt[0]
#3 = Total common items between queryList & listt[1]

我目前正在使用循环来执行此操作,但这似乎很慢。我将有数百万个列表,每个列表有数千个项目。

listt = [["a","abc","zzz","xxx","abc","abc"],["yyy","ggg","abc","cccc"]]
queryList = ["abc","cccc","abc","yyy"]

totalMatch = []
for hashtree in listt:
matches = 0
tempQueryHash = queryList.copy()
for hash in hashtree:
for i in range(len(tempQueryHash)):
if tempQueryHash[i]==hash:
matches +=1
tempQueryHash[i] = "" #Don't Match the same block twice.
break

totalMatch.append(matches)
print(totalMatch)

最佳答案

嗯,我仍在学习 Python 的基本知识。但是根据this较旧的帖子,类似以下内容应该有效:

from collections import Counter
listt = [["a","abc","zzz","xxx","abc","abc"],["yyy","ggg","abc","cccc"]]
queryList = ["abc","cccc","abc","yyy"]
OutputList = [len(list((Counter(x) & Counter(queryList)).elements())) for x in listt]
# [2, 3]

我会留意其他方法...

关于python - 比较两个列表中常见项目的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61142219/

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