找到多个集合的所有可能交点的最佳方法是什么?

时间:2019-06-11 20:27:54

标签: python set set-intersection

假设我有以下4套

ruby*:::require-return
{
  if (self->start)
    @counts["correlated require returns"] = count();
  else
    @counts["uncorrelated require returns"] = count();

  self->start = 0;
}

我想找到这些集合之间的所有可能的交集,例如:

ruby*:::require-return
{
  @counts[self->start ? "correlated require returns" : "uncorrelated require returns"] = count();
  self->start = 0;
}

就python而言,最佳方法是什么?谢谢!

3 个答案:

答案 0 :(得分:1)

来自here

# Python3 program for intersection() function 

set1 = {2, 4, 5, 6}  
set2 = {4, 6, 7, 8}  
set3 = {4,6,8} 

# union of two sets 
print("set1 intersection set2 : ", set1.intersection(set2)) 

# union of three sets 
print("set1 intersection set2 intersection set3 :", set1.intersection(set2,set3)) 

并从docs

  

交叉点(*其他)

     

设置和其他&...

     

返回一个新集合,其中包含该集合和所有其他集合的公共元素。

答案 1 :(得分:0)

您需要找到2组组合(从所需的输出中减去)。可以使用[Python 3.Docs]: itertools.combinations(iterable, r)来实现。对于每种组合,应执行2组之间的交集。
为了进行上述操作,将(输入)集“分组”在一个列表中(可迭代)。

还要指出[Python 3.docs]: class set([iterable])

code.py

#!/usr/bin/env python3

import sys
import itertools


def main():
    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7}
    set3 = {6, 7, 8, 9, 10}
    set4 = {1, 8, 9, 15}

    sets = [set1, set2, set3, set4]

    for index_set_pair in itertools.combinations(enumerate(sets, start=1), 2):
        (index_first, set_first), (index_second, set_second) = index_set_pair
        intersection = set_first.intersection(set_second)
        if intersection:
            print("Set{:d} and Set{:d} = {:}".format(index_first, index_second, intersection))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()
    print("\nDone.")

请注意,[Python 3.Docs]: Built-in Functions - enumerate(iterable, start=0)仅用于打印目的( Set 1 Set 2 ,...在输出中。)

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056551261]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32

Set1 and Set2 = {4, 5}
Set1 and Set4 = {1}
Set2 and Set3 = {6, 7}
Set3 and Set4 = {8, 9}

Done.

答案 2 :(得分:0)

如果仅查找两个集合的交集,则可以简单地嵌套for循环:

Set1 = {1,2,3,4,5}
Set2 = {4,5,6,7}
Set3 = {6,7,8,9,10}
Set4 = {1,8,9,15}
sets = [Set1,Set2,Set3,Set4]
for i,s1 in enumerate(sets[:-1]):
    for j,s2 in enumerate(sets[i+1:]):
        print(f"Set{i+1} and Set{i+j+2} = {s1&s2}")

# Set1 and Set2 = {4, 5}
# Set1 and Set3 = set()
# Set1 and Set4 = {1}
# Set2 and Set3 = {6, 7}
# Set2 and Set4 = set()
# Set3 and Set4 = {8, 9}

如果要查找这些集合中任意数量的交集,则可以使用itertools中的combinations()来生成一组强大的索引,并对每个组合执行交集:

from itertools import combinations
for comboSize in range(2,len(sets)):
    for combo in combinations(range(len(sets)),comboSize):
        intersection = sets[combo[0]]
        for i in combo[1:]: intersection = intersection & sets[i]
        print(" and ".join(f"Set{i+1}" for i in combo),"=",intersection)

Set1 and Set2 = {4, 5}
Set1 and Set3 = set()
Set1 and Set4 = {1}
Set2 and Set3 = {6, 7}
Set2 and Set4 = set()
Set3 and Set4 = {8, 9}
Set1 and Set2 = {4, 5}
Set1 and Set3 = set()
Set1 and Set4 = {1}
Set2 and Set3 = {6, 7}
Set2 and Set4 = set()
Set3 and Set4 = {8, 9}
Set1 and Set2 and Set3 = set()
Set1 and Set2 and Set4 = set()
Set1 and Set3 and Set4 = set()
Set2 and Set3 and Set4 = set()