Last iteration?
Raymond Hettinger
python at rcn.com
Wed Oct 17 03:16:36 EDT 2007
> 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)
Raymond
More information about the Python-list
mailing list