Possible Duplicate:
开发者_JAVA技巧How to find all possible subsets of a given array?
so say you have
ab
you can have a,b,ab
whats the best way of doing this?
From the itertools recipes
:
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
This is a standard python module, so reading that should give you insights on how its implemented and what algorithm is used. I don't know if it is the best, but it's an algorithm from the real world.
A power set is one solution
Power set of a set A is the set whose members are all possible subsets of A. For example, the powerset of {1, 2} is { {}, {1}, {2}, {1,2} } .
There's a Python solution:
What algorithm can calculate the power set of a given set?
精彩评论