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!