How to get all subsets of a set? (powerset)
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
00:28 Accepted answer (Score 264)
01:11 Answer 2 (Score 82)
02:28 Answer 3 (Score 26)
03:13 Answer 4 (Score 13)
03:36 Thank you
--
Full question
https://stackoverflow.com/questions/1482...
Accepted answer links:
[itertools]: https://docs.python.org/3/library/iterto...
Answer 3 links:
[Python Power Set Generator]: http://blog.technomancy.org/2009/3/17/a-...
Answer 4 links:
[powerset()]: https://more-itertools.readthedocs.io/en...
[more_itertools]: https://pypi.org/project/more-itertools/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #set #powerset
#avk47
ACCEPTED ANSWER
Score 295
The Python itertools page has exactly a powerset recipe for this:
from itertools import chain, combinations
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))
Output:
>>> list(powerset("abcd"))
[(), ('a',), ('b',), ('c',), ('d',), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd'), ('a', 'b', 'c'), ('a', 'b', 'd'), ('a', 'c', 'd'), ('b', 'c', 'd'), ('a', 'b', 'c', 'd')]
If you don't like that empty tuple at the beginning, you can just change the range statement to range(1, len(s)+1) to avoid a 0-length combination.
ANSWER 2
Score 85
Here is more code for a powerset. This is written from scratch:
>>> def powerset(s):
... x = len(s)
... for i in range(1 << x):
... print [s[j] for j in range(x) if (i & (1 << j))]
...
>>> powerset([4,5,6])
[]
[4]
[5]
[4, 5]
[6]
[4, 6]
[5, 6]
[4, 5, 6]
Mark Rushakoff's comment is applicable here: "If you don't like that empty tuple at the beginning, on."you can just change the range statement to range(1, len(s)+1) to avoid a 0-length combination", except in my case you change for i in range(1 << x) to for i in range(1, 1 << x).
Returning to this years later, I'd now write it like this:
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]
And then the test code would look like this, say:
print(list(powerset([4, 5, 6])))
Using yield means that you do not need to calculate all results in a single piece of memory. Precalculating the masks outside the main loop is assumed to be a worthwhile optimization.
ANSWER 3
Score 29
If you're looking for a quick answer, I just searched "python power set" on google and came up with this: Python Power Set Generator
Here's a copy-paste from the code in that page:
def powerset(seq):
"""
Returns all the subsets of this set. This is a generator.
"""
if len(seq) <= 1:
yield seq
yield []
else:
for item in powerset(seq[1:]):
yield [seq[0]]+item
yield item
This can be used like this:
l = [1, 2, 3, 4]
r = [x for x in powerset(l)]
Now r is a list of all the elements you wanted, and can be sorted and printed:
r.sort()
print r
[[], [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 4], [1, 3], [1, 3, 4], [1, 4], [2], [2, 3], [2, 3, 4], [2, 4], [3], [3, 4], [4]]
ANSWER 4
Score 14
from functools import reduce
def powerset(lst):
return reduce(lambda result, x: result + [subset + [x] for subset in result],
lst, [[]])