Consider the “string” ( treat it as an array of digits )
0 0 1 8 8 8 1 0
The RLE ( “groupby” ) is:
[(0,2), (1, 1), (8,3), (1, 1), (0, 1)]
We then enrich the above RLE with the sum of the run lengths of the previous elements.
Hence, the enriched version of the above becomes:
[(0, (0,2)), (0+2, (1, 1)), (0+2+1, (8,3)), (0+1+2+3, (1, 1)), (0+1+2+3+1, (0, 1))]
The “string” split on 1:
0 0 , 8 8 8 , 0
RLE split on 1
[(0,2)] , [(8,3)] , [(0, 1)]
The “string” split on 8:
0 0 1 , , , 1 0
RLE split on 8
[(0,2), (1, 1)] , , , [(1, 1), (0, 1)]
Note : In my examples, I have cited the “RLE split on Z” lists without enriching them. This would not be so. I left them out to reduce clutter. For example, the “RLE split on 1” should really be treated as:
[(0, (0,2))] , [(0+2+1, (8,3))] , [(0+1+2+3+1, (0, 1)]
How can I achieve this “RLE split” on Z ( = 1, 8; in this case )
It’s fine to leave out empty arrays ( after split ).
Perhaps a clever list comp.? ( it seems a little easier to solve with a for loop with an append nested within )
Just for show the way how, I strongly advise you not to use this
“elegant” ugly way: