python - Generate all possible permutations of subsets containing all the element of a set -


let s(w) set of words. want generate possible n-combination of subsets s union of subsets equal s(w).

so have set (a, b, c, d, e) , wan't 3-combinations:

((a, b, c), (d), (e))

((a, b), (c, d), (e))

((a), (b, c, d), (e))

((a), (b, c), (d, e))

etc ...

for each combination have 3 set , union of set original set. no empty set, no missing element.

there must way using itertools.combination + collection.counter can't start somewhere... can ?

luke

edit: need capture possible combination, including:

((a, e), (b, d) (c))

etc ...

something this?

from itertools import combinations, permutations t = ('a', 'b', 'c', 'd', 'e') slicer = [x x in combinations(range(1, len(t)), 2)] result = [(x[0:i], x[i:j], x[j:]) i, j in slicer x in permutations(t, len(t))] 

general solution, n , tuple length:

from itertools import combinations, permutations t = ("a", "b", "c") n = 2 slicer = [x x in combinations(range(1, len(t)), n - 1)] slicer = [(0,) + x + (len(t),) x in slicer] perm = list(permutations(t, len(t))) result = [tuple(p[s[i]:s[i + 1]] in range(len(s) - 1)) s in slicer p in perm]  [    (('a',), ('b', 'c')),    (('a',), ('c', 'b')),    (('b',), ('a', 'c')),    (('b',), ('c', 'a')),    (('c',), ('a', 'b')),    (('c',), ('b', 'a')),    (('a', 'b'), ('c',)),    (('a', 'c'), ('b',)),    (('b', 'a'), ('c',)),    (('b', 'c'), ('a',)),    (('c', 'a'), ('b',)),    (('c', 'b'), ('a',)) ] 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -