Reputation: 833
Let set A=set([1,2,3]) and set B=set()
Now I need to iteratively generate all possible combinations like
set([1])
set([2])
set([3])
set([1,2])
set([1,3])
set([2,3])
set([1,2,3])
I know blatantly I can use powergenerator recipe of itertools but the pseudo code is in below form to further conditions check(subset condition and Density condition)
a=set()
b=set([1,2,3])
for i in b-a:
a=a|set([i])
for j in a:
print a-set([j])
if den(a-set[j])>=0.6:#check density criteria
# check if a-set([j]) is subset of a on ordering criteria
The print statement of above i.e, print a-set([j]) has given output as below
set([])
set([2])
set([1])
set([2, 3])
set([1, 3])
set([1, 2])
but I need to have output in below format
set([1])
set([2])
set([3])
set([2, 3])
set([1, 3])
set([1, 2])
set([1,2,3])
Upvotes: 1
Views: 151
Reputation: 8692
try using iterto0ls to find the subset . itertools
import itertools
a=[1,2,3]
subsets=[]
for i in range(1,4):
for j in itertools.combinations(a,i):
subsets.append(list(j))
print subsets
#output=[[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
if set is method . u can link them,
map(set,subsets)
Upvotes: 0
Reputation: 58865
You can use itertools.combinations
:
from itertools import combinations
list(combinations(b, 1)) + list(combinations(b, 2)) + list(combinations(b, 3))
#[(1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
Upvotes: 1