Find combinations with arrays and a combination pattern
<p>I have arrays such as these, and each pattern designates a combination shape with each number representing the size of the combination.</p>

<ul>
<li>pattern 0: <code>[1, 1, 1, 1]</code></li>
<li>pattern 1: <code>[2, 1, 1]</code></li>
<li>pattern 2: <code>[3, 1]</code></li>
<li>pattern 3: <code>[4]</code></li>
<li>...</li>
</ul>

<p>I also have a char-valued list like below. len(chars) equals the sum of the upper array's value.</p>

<p><code>chars = ['A', 'B', 'C', 'D']</code></p>

<p>I want to find all combinations of chars following a given pattern. For example, for pattern 1, 4C2 * 2C1 * 1C1 is the number of combinations.</p>

<pre><code>[['A', 'B'], ['C'], ['D']]
[['A', 'B'], ['D'], ['C']]
[['A', 'C'], ['B'], ['D']]
[['A', 'C'], ['D'], ['B']]
[['A', 'D'], ['B'], ['C']]
[['A', 'D'], ['C'], ['B']]
...
</code></pre>

<p>But I don't know how to create such combination arrays. Of course I know there are a lot of useful functions for combinations in python. But I don't know how to use them to create a combination array of combinations.</p>

<p><strong>EDITED</strong></p>

<p>I'm so sorry my explanation is confusing. I show a simple example.</p>

<ul>
<li>pattern 0: <code>[1, 1]</code></li>
<li>pattern 1: <code>[2]</code></li>
<li><code>chars = ['A', 'B']</code></li>
</ul>

<p>Then, the result should be like below. So first dimension should be permutation, but second dimension should be combination.</p>

<ul>
<li>pat0: <code>[['A'], ['B']]</code></li>
<li>pat0: <code>[['B'], ['A']]</code></li>
<li>pat1: <code>[['A', 'B']]  # NOTE: [['B', 'A']] is same in my problem</code></li>
</ul>

<p>You can use recursive function that takes the first number in pattern and generates all the combinations of that length from remaining items. Then recurse with remaining pattern &amp; items and generated prefix. Once you have consumed all the numbers in pattern just <code>yield</code> the prefix all the way to caller:</p>

<pre><code>from itertools import combinations

pattern = [2, 1, 1]
chars = ['A', 'B', 'C', 'D']

def patterns(shape, items, prefix=None):
    if not shape:
        yield prefix
        return

    prefix = prefix or []
    for comb in combinations(items, shape[0]):
        child_items = items[:]
        for char in comb:
            child_items.remove(char)
        yield from patterns(shape[1:], child_items, prefix + [comb])

for pat in patterns(pattern, chars):
    print(pat)
</code></pre>

<p>Output:</p>

<pre><code>[('A', 'B'), ('C',), ('D',)]
[('A', 'B'), ('D',), ('C',)]
[('A', 'C'), ('B',), ('D',)]
[('A', 'C'), ('D',), ('B',)]
[('A', 'D'), ('B',), ('C',)]
[('A', 'D'), ('C',), ('B',)]
[('B', 'C'), ('A',), ('D',)]
[('B', 'C'), ('D',), ('A',)]
[('B', 'D'), ('A',), ('C',)]
[('B', 'D'), ('C',), ('A',)]
[('C', 'D'), ('A',), ('B',)]
[('C', 'D'), ('B',), ('A',)]
</code></pre>

<p>Note that above works only with Python 3 since it's using <code>yield from</code>.</p>

python combinations permutation