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

2024-04-24 22:44:45 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下4套

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

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

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

对于python来说,最好的方法是什么?谢谢您!你知道吗


Tags: and方法交点set1set2set3set4
3条回答

你需要找到2组组合(从你想要的输出中扣除)。这可以通过使用[Python 3.Docs]: itertools.combinations(iterable, r)来实现。对于每个组合,应执行两组之间的相交。
为了完成上述操作,(输入)集合被“分组”在一个列表(iterable)中。你知道吗

同时指出[Python 3.docs]: classset([iterable])。你知道吗

代码.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)仅用于打印目的(Set1Set2。。。在输出中)。你知道吗

输出

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

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开始:

intersection(*others)

set & other & ...

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

如果只查找两个集合的交集,只需执行嵌套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()

相关问题 更多 >