[英]Python 3.4 set intersection
I am trying to understand why my intersection does not return an empty list when I run this code. 我试图理解为什么我运行此代码时交集不返回空列表。
n = ([1,2,3,4,5],[3,4,5,6],[5,6,7],[7,8,9,10,11,12],[10,22,33,44,45])
w = set(n[0]).intersection(*n[:1])
print(w)
#Returns (1,2,3,4,5)
However this returns the correct set 但是,这将返回正确的集合
n = ([1,2,3,4,5],[3,4,5,6],[5,6,7],[7,8,9,10,11,12],[10,22,33,44,45])
w = set(n[0]).intersection(*n)
print(w)
#Returns empty set.
This question gave the correct results for both: 这个问题为两个都给出了正确的结果:
Python -Intersection of multiple lists? Python-多个列表的交集?
Why do I not get the correct result when I compare the first set to the remaining lists? 将第一组与其余列表进行比较时,为什么无法获得正确的结果?
*n[:1]
unpacks to (n[0],)
- the first element of the set. *n[:1]
解压缩为(n[0],)
-该集合的第一个元素。
So you're intersecting n[0]
with itself, and the result is what you see. 因此,您将
n[0]
与自身相交,结果就是您所看到的。 You probably intended to write 您可能打算写
set(n[0]).intersection(*n[1:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.