Last iteration?
Paul Hankin
paul.hankin at gmail.com
Wed Oct 17 07:12:45 EDT 2007
On Oct 17, 8:16 am, Raymond Hettinger <pyt... at rcn.com> wrote:
> > def lastdetecter(iterable):
> > "fast iterator algebra"
> > lookahead, t = tee(iterable)
> > lookahead.next()
> > t = iter(t)
> > return chain(izip(repeat(False), imap(itemgetter(1),
> > izip(lookahead, t))), izip(repeat(True),t))
>
> More straight-forward version:
>
> def lastdetecter(iterable):
> t, lookahead = tee(iterable)
> lookahead.next()
> return izip(chain(imap(itemgetter(0), izip(repeat(False),
> lookahead)), repeat(True)), t)
def lastdetector(iterable):
t, u = tee(iterable)
return izip(chain(imap(lambda x: False, islice(u, 1, None)),
[True]), t)
--
Paul Hankin
More information about the Python-list
mailing list