0

I'm a beginning programer looking to make a powerset for a list of numbers of varying size (generally at least 150+). The code I have works for smaller lists of numbers but not when I substitute the [4,5,6] for my larger list.

def powerset(s):
    x = len(s)
    masks = [1 << i for i in range(x)]
    for i in range(1 << x):
        yield [ss for mask, ss in zip(masks, s) if i & mask]

print(list(powerset([4, 5, 6])))

#this prints: [[], [4], [5], [4, 5], [6], [4, 6], [5, 6], [4, 5, 6]]

(This is not a homework question it is part of a larger code I am writing) Any help is super appreciated and please keep in mind that I have only been programming for a few months. Thank you!

Edit!: I realize now (thank you comments) that this would produce a ridiculously large list, New question: Is there a way to sort the powerset before it is generated. As in, instead of getting rid of any sets it produces with less than a certain number of numbers after the generator runs, is there a way to make it so the generator does not produce lists with certain characteristics (size, specific number, etc.) This way it would produce a significantly smaller amount of sets and I wouldn't have the storage issue I have now.

Lara PA
  • 1
  • 3
  • 1
    The order of the powerset of a 150 element set is... 2^150. – Jared Goguen Jul 11 '16 at 18:51
  • 1
    Your power set is going to have over a billion billion billion billion billion items in it. This is not a set you would ever have the time to generate or the memory to store. Whatever problem you're trying to solve by using this power set, find a smarter way to solve it. – user2357112 Jul 11 '16 at 18:51

0 Answers0