查找Python列表的交集/差集
我有两个Python列表:
a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
b = ['the', 'when', 'send', 'we', 'us']
我想把列表a中和列表b相似的元素过滤掉。比如在这个例子中,我应该得到:
c = [('why', 4), ('throw', 9), ('you', 1)]
最有效的方法应该是什么呢?
7 个回答
2
in
这个用法挺不错的,但至少在处理 b
的时候,你应该使用集合(sets)。如果你有用到 numpy 这个库,你也可以试试 np.in1d
,不过它到底快不快,你最好自己试一下。
# ruthless copy, but use the set...
b = set(b)
filtered = [i for i in a if not i[0] in b]
# with numpy (note if you create the array like this, you must already put
# the maximum string length, here 10), otherwise, just use an object array.
# its slower (likely not worth it), but safe.
a = np.array(a, dtype=[('key', 's10'), ('val', int)])
b = np.asarray(b)
mask = ~np.in1d(a['key'], b)
filtered = a[mask]
集合还有一些方法,比如 difference
等等,虽然在这里可能用不上,但一般来说还是挺有用的。
5
你可以用列表推导式来解决这个问题:
c = [item for item in a if item[0] not in b]
或者你也可以用字典推导式:
d = dict(a)
c = {key: value for key in d.iteritems() if key not in b}
11
可以使用列表推导式来解决这个问题。
a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
b = ['the', 'when', 'send', 'we', 'us']
filtered = [i for i in a if not i[0] in b]
>>>print(filtered)
[('why', 4), ('throw', 9), ('you', 1)]