I'm looking for an elegant 'Pythonic' way to loop through input in groups of a certain size. But the size of the groups vary and you don't know the size of a particular group until you start parsing.
Actually I only care about groups of two sizes. I have a large sequence of input which come in groups of mostly size X but occasionally size Y. For this example lets say X=3 and Y=4 (my actual problem is X=7 and Y=8). I don't know until mid-way through the group of elements if it's going to be a group of size X or of size Y elements.
In my problem I'm dealing with lines of input but to simplify I'll illustrate with characters.
So if it's a group of a particular size I know I'll always get input in the same sequence. So for example if it's a size X group I'll be getting elements of type [a,a,b] but if it's a size Y group I'll be getting elements of type [a,a,c,b]. f it's something of type 'a' I'll want to process it in a certain way and 'b' another etc.
Obviously I have to test an element at some point to determine if it's of type one group or the other. As demonstrated above I cannot just check the type of every element because there may be two of the same in sequence. Also the groups may be the same pattern at the start and only differ near the end. In this example the earliest I can check if I'm in a size X or size Y group is by testing the 3rd element (to see if it's of type 'c' or type 'b').
I have a solution with a for loop with an exposed index and two extra variables, but I'm wondering if there is a more elegant solution.
Here is my code. I've put pass
statements in place of where I would do the actual parsing depending on what type it is:
counter = 0
group = 3
for index, x in enumerate("aabaabaacbaabaacbaabaab"):
column = index - counter;
print(str(index) + ", " + x + ", " + str(column))
if column == 0:
pass
elif column == 1:
pass
elif column == 2:
if x == 'c':
pass
elif x == 'd':
group = 4
elif column == 3:
pass
if column + 1 == group:
counter += group
group = 3
In the code example the input stream is aabaabaacbaabaacbaabaab
so that is groups of:
- aab (3)
- aab (3)
- aacb (4)
- aab (3)
- aacb (4)
- aab (3)
- aab (3)