Lets assume I've got a list of integers:
mylist = [101, 102, 103, 104, 105, 106]
Now I need to create every possible sublist division (order preserved):
sublists = [([101], [102, 103, 104, 105, 106]),
([101, 102], [103, 104, 105, 106]),
([101, 102, 103], [104, 105, 106]),
...
([101, 102], [103, 104], [105, 106]),
...
([101], [102, 103, 104], [105], [106]),
...
([101], [102], [103], [104], [105], [106])]
Any idea? Would itertools
be helpful?
You are creating slice points; are you slicing after the current element or not. You can generate these with booleans:
Demo:
If you want to drop the last entry (containing a list with only one list in it, in turn containing all elements), replace the last 2 lines with:
This is an interesting question! Although Martijn has given an elegant answer, I'd still like to post mine that tackles this problem from another angle - Divide & Conquer:
Result: