Вы можете создать рекурсивный алгоритм для создания раздела:
import itertools
def part(lst, n):
if n <= 1:
# single answer for 1 partition
yield [lst]
else:
for m in range(len(lst)+1):
for head in itertools.combinations(lst, m):
nothead = [x for x in lst if x not in head]
for tail in part(nothead, n-1):
yield [list(head)]+tail
def partition(n):
lst = list(range(n))
for x in part(lst, n):
print(x)
Пример: partition(3)
:
[[], [], [0, 1, 2]]
[[], [0], [1, 2]]
[[], [1], [0, 2]]
[[], [2], [0, 1]]
[[], [0, 1], [2]]
[[], [0, 2], [1]]
[[], [1, 2], [0]]
[[], [0, 1, 2], []]
[[0], [], [1, 2]]
[[0], [1], [2]]
[[0], [2], [1]]
[[0], [1, 2], []]
[[1], [], [0, 2]]
[[1], [0], [2]]
[[1], [2], [0]]
[[1], [0, 2], []]
[[2], [], [0, 1]]
[[2], [0], [1]]
[[2], [1], [0]]
[[2], [0, 1], []]
[[0, 1], [], [2]]
[[0, 1], [2], []]
[[0, 2], [], [1]]
[[0, 2], [1], []]
[[1, 2], [], [0]]
[[1, 2], [0], []]
[[0, 1, 2], [], []]