I have a venn diagram consisting of the intersections of 6 different sets/lists (picture below). I was wondering if there is a way to compute the intersections using python. Currently in order generate the venn diagram you can see below I am using InteractiVenn. However, since its kind of painful to click every possible intersection on this venn diagram to retrieve the list of elements in this intersection I was wondering if there's a more efficient way.
Asked
Active
Viewed 36 times
0
-
Does this answer your question? [Best way to find the intersection of multiple sets?](https://stackoverflow.com/questions/2541752/best-way-to-find-the-intersection-of-multiple-sets) – Jan 10 '22 at 15:28
-
Hi thank you. It does help, however I have 6 different lists. Computing the intersection using ```intersection``` would take too much time writing right? – Alex L Jan 10 '22 at 15:31
2 Answers
1
This code will iterate over all the combinations choosing 2, 3, ..., 6
sets from the six sets and print the intersection of that combination.
I generated some random sets using the random module. You can replace with your sets.
import random
from itertools import combinations
def generate_list(length):
return [random.randint(1, 10) for _ in range(length)]
sets = {
'SetA': set(generate_list(4)),
'SetB': set(generate_list(22)),
'SetC': set(generate_list(47)),
'SetD': set(generate_list(45)),
'SetE': set(generate_list(51)),
'SetF': set(generate_list(33))
}
for choose in range(2, len(sets) + 1):
print('Choosing {} set(s) from {} sets'.format(choose, len(sets)))
for combination in combinations(sets.keys(), choose):
print('Intersection between {}:'.format(', '.join(combination)))
print(set.intersection(*tuple(sets[k] for k in combination)))
print()

rex
- 124
- 7
0
take a look at the following question
Best way to find the intersection of multiple sets?
you can use
u = set.intersection(s1, s2, s3)

Alik.Koldobsky
- 334
- 1
- 10