2

Possible Duplicate:
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?

Community
  • 1
  • 1
hao
  • 21
  • 1

2 Answers2

2

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.

miku
  • 181,842
  • 47
  • 306
  • 310
0

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?

Community
  • 1
  • 1
Chris S
  • 64,770
  • 52
  • 221
  • 239