[英]Intersection of two lists including duplicates?
>>> a = [1,1,1,2,3,4,4]
>>> b = [1,1,2,3,3,3,4]
[1,1,2,3,4]
請注意,這與以下問題不同: Python 兩個列表保持重復的交集因為即使列表 a 中有三個 1,但列表 b 中只有兩個,所以結果應該只有兩個。
您可以為此使用
collections.Counter<\/code><\/a> ,它會在您選擇交叉點時為每個元素提供在任一列表中找到的最低計數。
from collections import Counter
c = list((Counter(a) & Counter(b)).elements())
簡單,無需額外導入且易於調試 :)
缺點:列表b的值改變了。 如果您不想更改 b,請處理 b 的副本。
c = list()
for x in a:
if x in b:
b.remove(x)
c.append(x)
使用 Counter 發布的公認解決方案很簡單,但我認為這種使用字典的方法也可以工作並且可以更快 - 即使在未排序的列表上也是如此(該要求並未真正提及,但至少其中一個)解決方案假設是這種情況)。
a = [1, 1, 1, 2, 3, 4, 4]
b = [1, 1, 2, 3, 3, 3, 4]
def intersect(nums1, nums2):
match = {}
for x in a:
if x in match:
match[x] += 1
else:
match[x] = 1
i = []
for x in b:
if x in match:
i.append(x)
match[x] -= 1
if match[x] == 0:
del match[x]
return i
def intersect2(nums1, nums2):
return list((Counter(nums1) & Counter(nums2)).elements())
timeit intersect(a,b)
100000 loops, best of 3: 3.8 µs per loop
timeit intersect2(a,b)
The slowest run took 4.90 times longer than the fastest. This could mean
that an intermediate result is being cached.
10000 loops, best of 3: 20.4 µs per loop
這也應該有效。
a = [1, 1, 1, 2, 3, 4, 4]
b = [1, 1, 2, 3, 3, 3, 4]
c = []
i, j = 0, 0
while i < len(a) and j < len(b):
if a[i] == b[j]:
c.append(a[i])
i += 1
j += 1
elif a[i] > b[j]:
j += 1
else:
i += 1
print(c) # [1, 1, 2, 3, 4]
這也應該有效:
def list_intersect(lisA, lisB):
""" Finds the intersection of 2 lists including common duplicates"""
Iset = set(lisA).intersection(set(lisB))
Ilis = []
for i in Iset:
num = min(lisA.count(i), lisB.count(i))
for j in range(num):
Ilis.append(i)
return Ilis
這將做:
from itertools import chain
list(chain.from_iterable([(val,)*min(a.count(val), b.count(val)) for val in (set(a) & set(b))]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.