Python: I have a string and a list of lists of varying lengths and I want to return the letters in groups as they correspond to the list
<p>Imagine that I have a string that is 5 letters long, say 'ABCDE'.  I also have a list of lists of the different way to split the list i.e. [[5], [4, 1], [3, 2], ... [1, 1, 1, 1, 1]].  How could return all the different ways to split the list, like below.  The problem I am having is setting up a loop with uneven numbers of indices.</p>

<pre><code>INPUT
'ABCDE', list_of_configurations

OUTPUT
['ABCDE']
['A', 'BCDE']
['AB', 'CDE']
...
...
['A', 'B', 'C', 'D', 'E']
</code></pre>

<p>Final note, I want it be dynamic in that it can apply to a string of 5 characters or a string of 9 characters.  I was trying to figure this out but I think it is beyond my current skill level.</p>

<p>That is a classic brute force problem. You have to generate all options of where you can split the string. You can either split it between each two letters or not. </p>

<p>For string of length n there are n - 1 places you can possibly split it</p>

<pre><code>a b c d e
 ^ ^ ^ ^
</code></pre>

<p>Thus, each binary (with only 0s (if you do not want to split string at that location) and 1s (if you want to split string at that location)) array of length <code>n - 1</code> corresponds to one possible split option. I.e.</p>

<pre><code>[1, 0, 0, 0] &lt;=&gt; ['a', 'bcde']
[1, 0, 1, 0] &lt;=&gt; ['a', 'bc', 'de']
</code></pre>

<p>From each array you can generate the splitting. Thus, the problem is equivalent to looping through all the possible binary arrays.</p>

<p>You could do it using brute force recursion, but that is much easier to do just looping through the numbers from 0 to <code>2 ^ (n - 1)</code> and generating array based on binary representation of this number.</p>

<p>With all this said, that is the code that solves your problem.</p>

<pre><code>def gen_all_splits(s):
    result = []
    for i in range(2 ** (len(s) - 1)):
        split = bin(i).replace('0b', '')
        split = '0' * (len(s) - len(split)) + split
        current_string_split = ['']
        for j in range(len(split)):
            if split[j] == '0':
                current_string_split[-1] += s[j]
            else:
                current_string_split.append(s[j])
        result.append(current_string_split)
    return result

print(gen_all_splits('abcdef'))
</code></pre>

<hr>

<p>P.S. Thanks to comment, I just realized that I answered to broader problem that yours are. I will keep the answer, but you can use the piece relevant to generating the string by split configuration:</p>

<pre><code>        current_string_split = ['']
        for j in range(len(split)):
            if split[j] == '0':
                current_string_split[-1] += s[j]
            else:
                current_string_split.append(s[j])
</code></pre>

python string list