0

I want to get all possible groupings. All elements have to be assigned. If I use itertools.permutations, I miss some groups:

from itertools import permutations, chain

testlist = [1, 2, 3]
print(all_group_assignments(testlist))


def all_group_assignments(list):

    groups = []
    for i in range(0, len(list)+1):
        for splits in permutations(range(1, len(list)), i):
            prev = None
            result = []
            for split in chain(splits, [None]):
                result.append(list[prev:split])
                prev = split
            groups.append(result)

    return groups

I receive this as result:

[[[1, 2, 3]], [[1], [2, 3]], [[1, 2], [3]], [[1], [2], [3]], [[1, 2], [], [2, 3]]]

which misses the group [[1, 3], [2]] and instead includes [[1, 2], [], [2, 3]] which I don't need.

Is there a way to solve this elegantly? Thanks in advance!

1 Answers1

0

You can use itertools.combinations for the task.

For example:

from itertools import combinations

lst = [1, 2, 3]

out = [[[i] for i in lst]]
for i in range(2, len(lst)+1):
    for p in combinations(out[0], i):
        out.append([])
        group, seen = sum(p, []), set()
        for v in out[0]:
            if v[0] not in group and v[0] not in seen:
                out[-1].append(v)
            elif v[0] in group and v[0] not in seen:
                out[-1].append(group)
                seen = set(group)
                group = []
print(out)

Prints:

[[[1], [2], [3]], [[1, 2], [3]], [[1, 3], [2]], [[1], [2, 3]], [[1, 2, 3]]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • thanks for this idea! it works for a list of three, but for bigger lists is only returns groups assignments with one group being bigger than the size of 1. A list of 4 elements could also be split into two groups of two which is not done by this algorithm – floherian Jun 03 '20 at 21:55