On Apr 19, 9:13 am, Anton Vredegoor <[EMAIL PROTECTED]> wrote:
(snipped) > > > How about this one? > > No that can result in an infinite loop after yet another > > print it1.next() > > This one however ... > > from collections import deque > > class sentinel(object): > pass > > class myiter(object): > > def __init__(self,seq): > self.seq = seq > self.index = -1 > > def __iter__(self): > return self > > def next(self): > self.index +=1 > if self.index < len(self.seq): > return self.seq[self.index] > else: > return sentinel > > def xsplitter(seq, pred): > Q = deque(),deque() > it = myiter(seq) > def gen(p): > for x in it: > while Q[p]: yield Q[p].popleft() > if x is sentinel: break > if pred(x) == p: yield x > else: > Q[~p].append(x) > for x in gen(p): yield x > return gen(1),gen(0) > > def test(): > L = 'a', 1, 2, 'a' > it1, it2 = xsplitter(L, lambda x: x == 'a') > print it1.next() > print it2.next() > print it1.next() > print it2.next() > > if __name__=='__main__': > test() > > A. Um, no. That one stops prematurely if your input sequence is: L = 1, 2, 3, 'a', 'a' You get points for persistence, however. :) -- Regards, Steven -- http://mail.python.org/mailman/listinfo/python-list