查找两个可迭代对象共享的匹配值数量的更有效方法? [英] More efficient ways to find the number of matching values shared by two iterables?
问题描述
寻找匹配项而不是匹配项本身.无法用集合或[x for x in list1 if x in list2]
类型的方式求解. list1.count(x) if x in list2
可以.
Looking for the number of matches not the matches themselves. Cannot solve with sets or [x for x in list1 if x in list2]
type manner. list1.count(x) if x in list2
works though.
比方说,您有两个列表,分别为list1和list2,并希望查找list1中的值与list2中的值相匹配的次数.
Let's say you have two lists, list1 and list2, and want to find the number of times a value from list1 matches a value from list2.
我使用以下代码成功完成了此任务-
I used the following code to perform this task successfully -
sum([x==y for x in list1 for y in list2])
问题是此代码无法有效处理较大的列表.有没有比"double for"循环更快,更有效的方法了,我敢说比double for循环更多的pythonic方法来解决这个问题?
The problem is this code cannot handle larger lists efficiently. Is there a faster, more efficient, dare I say more pythonic way to solve this problem than the "double for" loop?
推荐答案
计数器使用&
运算符支持多集交集:
Counters support multiset intersections with the &
operator:
>>> from collections import Counter
>>> list1 = list("abba")
>>> list2 = list("bbanana")
>>> c1 = Counter(list1)
>>> c2 = Counter(list2)
>>> sum(c1[k]*c2[k] for k in c1 & c2) # O(n)
10
>>> sum([x==y for x in list1 for y in list2]) # O(n**2)
10
这篇关于查找两个可迭代对象共享的匹配值数量的更有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!