简体   繁体   English

python中的三组交集?

[英]intersection of three sets in python?

Currently I am stuck trying to find the intersection of three sets. 目前我被困在试图找到三组的交集。 Now these sets are really lists that I am converting into sets, and then trying to find the intersection of. 现在这些集合实际上是我要转换成集合的列表,然后尝试找到它的交集。

Here's what I have so far: 这是我到目前为止所拥有的:

for list1 in masterlist:
    list1 = thingList1
for list2 in masterlist:
    list2 = thingList2
for list3 in masterlist:
    list3 = thingList3

d3 = [set(thingList1), set(thingList2), set(thingList3)] 
setmatches c = set.intersection(*map(set,d3)) 
print setmatches

and I'm getting 而且我正在

set([]) 
Script terminated.

I know there's a much simpler and better way to do this, but I can't find one... 我知道有一个更简单,更好的方法来做到这一点,但我找不到一个......

EDIT 编辑

Okay, here's what I have now. 好的,这就是我现在所拥有的。

setList=()
setList2=()
setList3=()

for list1 in masterlist:
    setList=list1
    for list2 in masterlist:
        setList2=list2
        for list3 in masterlist:
            setList3=list3



setmatches=set(setList) & set(setList2) & set(setList3)
print setmatches

Still doesn't give me what I'm looking for: which is the one match I ensured was in each list. 仍然没有给我我正在寻找的东西:这是我确保在每个列表中的一个匹配。 It's giving me what looks like an addition of all the sets. 它给了我看起来像是所有套装的补充。

I think you are simply looking for: 我想你只是在寻找:

set(thingList1) & set(thingList2) & set(thingList3)

The ampersand is intersection in Python (and some other languages as well). &符是Python(以及其他一些语言)的交集。

set1 & set2 & set3

should work ... at least I think 应该工作......至少我认为

>>> set((1,2,3)) & set((2,3,4)) & set((3,4,5))
set([3])

你需要这样的东西:

frozenset(list1) & frozenset(list2) & frozenset(list1)

这应该做的伎俩:

reduce(lambda x,y: x&y, mysetlist)

A list of sets, you say? 你说的套装清单?

In [1]: mylist = [ [1, 2, 3, 4], [3, 4, 5, 6, 7], [2, 3, 4, 5, 6] ]

In [2]: result = set(mylist[0])

In [3]: for item in mylist:
   ...:     result = result.intersection(item)
   ...:     
   ...:     

In [4]: result
Out[4]: set([3, 4])
set.intersection(*map(set,d3)) 

Will actually work, though because d3 already contains sets you can just do: 实际工作,但因为d3已经包含集,你可以这样做:

set.intersection(*d3)

And, in fact, only the first one needs to be a set - the others can be any iterable, and intersection will setify them by itself. 而且,事实上,只有第一个需要成为一个set - 其他可以是任何可迭代的, intersection将自己整理它们。

The problem you're having doesn't seem to be in this code - rather, 您遇到的问题似乎不在此代码中 - 相反,

for list1 in masterlist:
    list1 = thingList1

Won't actually put anything into thingList1 . 实际上不会将任何东西放入thingList1 It is hard to tell without seeing what masterlist looks like, but you may want something like: 没有看到什么样的masterlist看起来很难说,但你可能需要这样的东西:

for list1 in masterlist:
   thingList1[:] = list1

print your three lists before you do the intersection to make sure they contain what you expect. 在做交叉点之前print三个列表以确保它们包含您期望的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM