Feb-05-2022, 06:00 PM
(This post was last modified: Feb-05-2022, 06:01 PM by deanhystad.)
Don't use the reply button. It is annoying having to scroll down a bunch of duplicated posts.
Here is a solution that uses itertools combinations. It is a generator, so it only makes the subsets one at a time
Here is a solution that uses itertools combinations. It is a generator, so it only makes the subsets one at a time
import itertools def powerset(values): """Generate subsets of values.""" for length in range(len(values)+1): for subset in itertools.combinations(values, length): yield subset for subset in powerset((1, 2, 3)): print(subset)
Output:()
(1,)
(2,)
(3,)
(1, 2)
(1, 3)
(2, 3)
(1, 2, 3)