Lets say I have these numeric sets
a = {1, 2, 3}
b = {2, 3, 4}
c = {1, 5}
I want to find all distinct numeric groupings of the sets. The result would be
{1}, {2, 3}, {4}, {5}
My naive approach, which doesn't work, is something like this:
data = [{1, 2, 3}, {2, 3, 4}, {1, 5}]
for i in range(1, 5):
s = set.intersection(*[x for x in data if i in x])
print(s)
Which returns
set([1])
set([2, 3])
set([2, 3])
set([2, 3, 4])
Which could be easily de-duplicated but doesn't give the expected result.
How can I get only the groupings of numbers that exist in subset of sets?
Your code has two issues:
5, but range doesn't include the stop so you don't check for 5.So by fixing these issues the code would look like this:
data = [{1, 2, 3}, {2, 3, 4}, {1, 5}]
for i in range(1, 6):
useful_sets = [x for x in data if i in x]
if len(useful_sets) <= 1:
print(set([i]))
else:
s = set.intersection(*useful_sets)
print(s)
# prints:
# {1}
# {2, 3}
# {2, 3}
# {4}
# {5}
To get a complete (and not duplicated) result you could store them as frozensets in a set:
data = [{1, 2, 3}, {2, 3, 4}, {1, 5}]
res = set()
for i in range(1, 6):
useful_sets = [x for x in data if i in x]
if len(useful_sets) <= 1:
res.add(frozenset([i]))
else:
s = set.intersection(*useful_sets)
res.add(frozenset(s))
print(res)
# {frozenset({5}), frozenset({4}), frozenset({2, 3}), frozenset({1})}
Which (except for the ordering) should be exactly what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With