Re: multirember&co



On Apr 19, 9:13 am, Anton Vredegoor <anton.vredeg...@xxxxxxxxx> 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


.



Relevant Pages

  • Re: Just for fun: Countdown numbers game solver
    ... Ops is vanilla, cleverops only returns canonical expressions as defined in Dan's email. ... def getop: return 'n' if isinstance ... yield x + y, ... "Return all ways of reaching target with nums" ...
    (comp.lang.python)
  • Re: Nested generator caveat
    ... Here's what's actually going on in your generator. ... def gen1: ... yield i, gen1 ... the for loop in gen0 is suspended each iteration while we do some ...
    (comp.lang.python)
  • Re: pre-PEP: Simple Thunks
    ... change Your thunk somehow e.g. from ... def yield_thunk: ... This is the reason why those ...
    (comp.lang.python)
  • Re: can Python be useful as functional?
    ... def sieve: ... I also know that Python got some useful tool such as map, filter, ... These should be almost the same: listprimes actually lists prime ...
    (comp.lang.python)
  • Re: implementing the "each" method for own classes
    ... >>> def each ... >>> yield node.content ... >> You are missing the first element. ...
    (comp.lang.ruby)