Generate all possible sublists of a list by keys
<p>I have data like </p>

<pre><code>[2, 2, 2, 2, 2, 3, 13, 113]
</code></pre>

<p>which I then want to sort into separate lists by keys generated by myself. In fact I want to generate <em>all</em> possible lists. </p>

<p>Some examples:</p>

<pre><code>values: [2, 2, 2, 2, 2, 3, 13, 113]
keys:   [0, 0, 1, 2, 1, 3,  3,   1]
sublists: [2, 2], [2, 2, 113], [2], [3, 13]

values: [2, 2, 2, 2, 2, 3, 13, 113]
keys:   [0, 1, 0, 0, 0, 1,  1,   0]
sublists: [2, 2, 2, 2, 113], [2, 3, 13]

values: [2, 2, 2, 2, 2, 3, 13, 113]
keys:   [2, 3, 0, 0, 4, 4,  1,   3]
sublists: [2, 2], [13], [2], [2, 113], [2, 3]
</code></pre>

<p>All possible keys are generated by</p>

<pre><code>def generate_keys(prime_factors):
    key_size = len(prime_factors) - 1
    key_values = [str(i) for i in range(key_size)]

    return list(itertools.combinations_with_replacement(key_values, \
        len(prime_factors)))
</code></pre>

<p>Then I thought I could use the keys to shift the values into the sublists. That's the part I'm stuck on. I thought itertools.groupby would be my solution but upon further investigation I see no way to use my custom lists as keys for groupby. </p>

<p>How do I split my big list into smaller sublists using these keys? There may even be a way to do this without using keys. Either way, I don't know how to do it and looking at other Stack Overflow questions has eben in the ballpark but not exactly this question.</p>
