[英]Get complement of three lists
I have three lists: 我有三个清单:
a = [1, 2, 3, 4] b = [2, 3, 4, 5] c = [3, 4, 5, 6]
My goal is to get all values which are not present in all of the three lists: [1,2,5,6]
我的目标是获得三个列表中不存在的所有值:
[1,2,5,6]
Basically I am searching the "negation" of set(a) & set(b) & set(c)
. 基本上我正在搜索
set(a) & set(b) & set(c)
的“否定”。
An efficient solution would be appreciated since the lists are very long. 由于列表很长,因此可以理解有效的解决方案。
The opposite of set(a) & set(b) & set(c)
is already explained in this question as stated by Chris_Rands in the comments: 正如Chris_Rands在评论中所阐述的那样,已经在这个问题中解释了
set(a) & set(b) & set(c)
的反面 :
>>> (set(a) | set(b) | set(c)) - (set(a) & set(b) & set(c))
{1, 2, 5, 6}
For really long lists, using numpy should be may efficient: 对于很长的列表,使用numpy应该可以有效:
import numpy as np
from functools import reduce
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
c = [3, 4, 5, 6]
union = reduce(numpy.union1d, (a,b,c))
intersect = reduce(numpy.intersect1d, (a,b,c))
print(numpy.setdiff1d(union, intersect))
Output: 输出:
[1 2 5 6]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.