基本上我想要的是这样的:
>>> a = ["a","a","b","c","c","c","d","e","f"]
>>> b = ["a","b","c","d","e","f"]
>>> #Do something, something like a - b
>>> result = ["a","c","c"]
我想这样做的原因是,我加入了一堆偏好列表,并想在许多列表中找出哪一个是常见的。它们在列表 a 中出现的次数越多(因为更多列表具有该元素),我对它的权重就越大
基本上我想要的是这样的:
>>> a = ["a","a","b","c","c","c","d","e","f"]
>>> b = ["a","b","c","d","e","f"]
>>> #Do something, something like a - b
>>> result = ["a","c","c"]
我想这样做的原因是,我加入了一堆偏好列表,并想在许多列表中找出哪一个是常见的。它们在列表 a 中出现的次数越多(因为更多列表具有该元素),我对它的权重就越大
你正在寻找多组,真的。使用collections.Counter()
,多重集的 Python 实现:
from collections import Counter
acount = Counter(a)
bcount = Counter(b)
result = list((acount - bcount).elements())
演示:
>>> from collections import Counter
>>> a = ['a', 'a', 'b', 'c', 'c', 'c', 'd', 'e', 'f']
>>> b = ['a', 'b', 'c', 'd', 'e', 'f']
>>> Counter(a) - Counter(b)
Counter({'c': 2, 'a': 1})
>>> list((Counter(a) - Counter(b)).elements())
['a', 'c', 'c']
但是,您可能希望保留这些Counter()
实例;但是,如果您需要它,该Counter.elements()
方法会生成一系列元素乘以它们的计数,以再次产生您想要的输出。
您需要做的就是遍历 b 的每个元素并将其从 a 中删除。
在不使用多重集的情况下,您可以遍历 b 的元素并从 a 中删除。Remove 仅删除元素的单个实例,而不是等效元素的所有实例。您可以使用 map 简洁地做到这一点。
result = a[:] #copy a
map(result.remove, b) #remove elements of b from a