1

I want a python program that takes a list of integers and returns a list of lists of tuples that contain the subsets as well as the subsets with the rest of the values not used in the first subset, essentially getting all possibilities to combine the values in the list.

[1,2,3,4]

should return

[[(1,), (2,), (3,), (4,)], [(1,), (2,3,), (4,)], [(1,), (2,) (3,4,)], [(1,), (2,4,), (3,)],[(1,), (2,3,4,)], [(1,2,), (3,), (4,)], [(1,2,), (3,4)]...and so on]
VirtualScooter
  • 1,792
  • 3
  • 18
  • 28

1 Answers1

0

Yes. This is surprisingly easy with library more_itertools. You can use the for loop below to write your tuple level output and discard undesired results.

import more_itertools

s1 = set([1,2,3,4])
subsubsets = more_itertools.set_partitions(s1)
print(*subsubsets)
subsubsets = more_itertools.set_partitions(s1)
for l in subsubsets:
    if l1 != sum(len(k) for k in l):
        print(f"{l} non-compliant")
        break

Output (added extra linebreaks)

[[1, 2, 3, 4]]
[[1], [2, 3, 4]]
[[1, 2], [3, 4]]
[[2], [1, 3, 4]]
[[1, 2, 3], [4]]
[[2, 3], [1, 4]]
[[1, 3], [2, 4]]
[[3], [1, 2, 4]]
[[1], [2], [3, 4]]
[[1], [2, 3], [4]]
[[1], [3], [2, 4]]
[[1, 2], [3], [4]]
[[2], [1, 3], [4]]
[[2], [3], [1, 4]]
[[1], [2], [3], [4]]
VirtualScooter
  • 1,792
  • 3
  • 18
  • 28