简体   繁体   中英

What is the best way to find ALL possible intersections of multiple sets?

Let's say i have the following 4 sets

Set1 = {1,2,3,4,5}
Set2 = {4,5,6,7}
Set3 = {6,7,8,9,10}
Set4 = {1,8,9,15}

I want to find all possible intersections between any of these sets, such as:

Set1 and Set4: 1
Set1 and Set2: 4,5
Set2 and Set3: 6,7
Set3 and Set4: 8,9

What's the best approach for this in terms of python? Thank you!

From 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)) 

And from the docs :

intersection(*others)

set & other & ...

Return a new set with elements common to the set and all others.

You need to find the 2 set combinations (deducted that from your desired output). That can be achieved using [Python 3.Docs]: itertools. combinations ( iterable, r ) . For each combination, the intersection between the 2 sets should be performed.
In order to do the above, the (input) sets are "grouped" in a list (iterable).

Also pointing out [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.")

Note that [Python 3.Docs]: Built-in Functions - enumerate ( iterable, start=0 ) is used for printing purposes only ( Set 1 , Set 2 , ... in the output).

Output :

 [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.

If you're only looking for intersections of two sets, you can simply do nested for loops:

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}

If you're looking for intersections of any number of these sets then you can use combinations() from itertools to produce a power set of indices and perform the intersection for each combination:

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()
  • A "power set" is when you take all possible combinations of various sizes from a set of values. itertools documentation has a recipe for that.
  • In this case we are only interested in combinations of 2,3,..., n-1 sizes. Hence the loop on comboSize in range(2,len(sets))
  • For each of these sizes, we get combination of indexes in the sets list using itertool's combinations function. eg for comboSize=3 and 4 items in sets, combo will get: (0, 1, 2) (0, 1, 3) (0, 2, 3) (1, 2, 3)
  • The intersection set will intersect all the sets at these indexes using the & operator (set intersection) starting with the first index ( combo[0] ) and intersecting the remaining indexes ( combo[1:] ) into a single set.
  • The print function joins the set identifiers ( f"Set{i+1}" ) with an " and " string and prints the resulting intersection on the same line.
  • f"Set{i+1}" is a format string that replaces {i+1} with the set index from combo (+1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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