Let's say I have a sequence that goes like this:
seq = (1, 1, 1, 1, 4, 6, 8, 4, 3, 3, 3,)
Some arbitrary number of 1s, followed by some arbitrary number of even numbers, followed by some 3s. If I try to split it up like so:
it = iter(seq)
ones = list(takewhile(lambda x: x == 1, it))
evens = list(takewhile(lambda x: x%2 == 0, it))
threes = list(takewhile(lambda x: x == 3, it))
This almost works out... except I miss the first even number and the first three since it's already used up by takewhile
. Is there a way to do this kind of partitioning by just walking the iterator forward, predicate by predicate?