Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shallowcopy of an object
#4
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
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)
Reply


Messages In This Thread
shallowcopy of an object - by usercat123 - Feb-04-2022, 08:30 PM
RE: shallowcopy of an object - by deanhystad - Feb-05-2022, 05:13 AM
RE: shallowcopy of an object - by usercat123 - Feb-05-2022, 01:52 PM
RE: shallowcopy of an object - by deanhystad - Feb-05-2022, 06:00 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020