Calculating combinations of subsets of given size leading up to the set
Question:
I would like to get all the possible subsets of a given length, that lead up to the real set. So for the set [a,b,c] and subset sizes 1 & 2 I would like to calculate:
[[a,b],[c]] , [[b,c],[a]] , [[a,c],[b]]
I’ve been experimenting with code from Algorithm to calculate power set (all possible subsets) of a set in R , but that algorithm will generate all of the subsets, and not just those leading up to the set.
I’m trying to solve this problem for size ~30. I want to calculate the score of each combination of subsets and only keep the best ones, this way I hope memory is no difficulty. The programming language of choice is python.
Answers:
How about creating the combinations of length 2, and generate the remainder by computing the difference from the original set? Here’s what I mean:
from itertools import combinations
s = {'a', 'b', 'c'}
res = [(set(comb), s.difference(comb)) for comb in combinations(s, 2)]
Generates:
[({'a', 'c'}, {'b'}), ({'b', 'c'}, {'a'}), ({'a', 'b'}, {'c'})]
You can use following code for finding size of subset inside list:
list1=[[1,2,3],[1,3],[2,3,3,4]]
x= len(list1)
while(x>0):
print len(list1[x-1])
x=x-1
output:
4
2
3
I would like to get all the possible subsets of a given length, that lead up to the real set. So for the set [a,b,c] and subset sizes 1 & 2 I would like to calculate:
[[a,b],[c]] , [[b,c],[a]] , [[a,c],[b]]
I’ve been experimenting with code from Algorithm to calculate power set (all possible subsets) of a set in R , but that algorithm will generate all of the subsets, and not just those leading up to the set.
I’m trying to solve this problem for size ~30. I want to calculate the score of each combination of subsets and only keep the best ones, this way I hope memory is no difficulty. The programming language of choice is python.
How about creating the combinations of length 2, and generate the remainder by computing the difference from the original set? Here’s what I mean:
from itertools import combinations
s = {'a', 'b', 'c'}
res = [(set(comb), s.difference(comb)) for comb in combinations(s, 2)]
Generates:
[({'a', 'c'}, {'b'}), ({'b', 'c'}, {'a'}), ({'a', 'b'}, {'c'})]
You can use following code for finding size of subset inside list:
list1=[[1,2,3],[1,3],[2,3,3,4]]
x= len(list1)
while(x>0):
print len(list1[x-1])
x=x-1
output:
4
2
3