Ask Your Question

Revision history [back]

What is missing from the OrderedSetPartitions are the positions of the additional empty sets. Chosing a subset of size i in a list of size k is done via Combinations. So, here is a possibility mixing those two tools:

def OrderedSetPartitions_0(n,k):
    my_list = []
    for i in range(1,k+1):
        for empty_spots in Combinations(k,k-i):
            for part in OrderedSetPartitions(range(n),i):
                L = list(part)
                LL = []
                for j in range(k):
                    if j in empty_spots:
                        LL.append(set())
                    else:
                        LL.append(L.pop())
                my_list.append(LL)
    return my_list

For example you get as expected:

sage: OrderedSetPartitions_0(3,3)
[[set(), set(), {0, 1, 2}],
 [set(), {0, 1, 2}, set()],
 [{0, 1, 2}, set(), set()],
 [set(), {2}, {0, 1}],
 [set(), {1}, {0, 2}],
 [set(), {0}, {1, 2}],
 [set(), {1, 2}, {0}],
 [set(), {0, 2}, {1}],
 [set(), {0, 1}, {2}],
 [{2}, set(), {0, 1}],
 [{1}, set(), {0, 2}],
 [{0}, set(), {1, 2}],
 [{1, 2}, set(), {0}],
 [{0, 2}, set(), {1}],
 [{0, 1}, set(), {2}],
 [{2}, {0, 1}, set()],
 [{1}, {0, 2}, set()],
 [{0}, {1, 2}, set()],
 [{1, 2}, {0}, set()],
 [{0, 2}, {1}, set()],
 [{0, 1}, {2}, set()],
 [{2}, {1}, {0}],
 [{1}, {2}, {0}],
 [{2}, {0}, {1}],
 [{1}, {0}, {2}],
 [{0}, {2}, {1}],
 [{0}, {1}, {2}]]