def gen():
    B1 = [1, 2, 3]
B2 = [7, 8]
C = [x for x in range(10) if x not in B1 + B2]

def gen1():
    for x in C:
        c = C[:]
b1 = B1[:]
b1.append(x)
c.remove(x)
for y in c:
    c1 = c[:]
b2 = B2[:]
b2.append(y)
c1.remove(y)
yield b1, b2, c1

def gen2():
    for i in range(len(C) - 1):
        for j in range(i + 1, len(C)):
            b2 = B2 + [C[i], C[j]]
c = [C[k] for k in range(len(C)) if k not in (i, j)]
yield B1, b2, c

def gen3():
    for x in C:
        b2 = B2[:]
c = C[:]
c.remove(x)
b2.append(x)
yield B1, b2, c
for g in (gen1, gen2, gen3):
    for t in g():
        yield t
Set partition with constraints in Python