Re: Last iteration?

2007-10-19 Thread Gabriel Genellina
En Fri, 19 Oct 2007 19:12:49 -0300, Michael J. Fromberger <[EMAIL PROTECTED]> escribió: > Before I affront you with implementation details, here's an example: > > | from __future__ import with_statement > > | with last_of(enumerate(file('/etc/passwd', 'rU'))) as fp: > | for pos, line in fp:

Re: Last iteration?

2007-10-19 Thread Michael J. Fromberger
In article <[EMAIL PROTECTED]>, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > [Diez B. Roggisch] > > > out:) But I wanted a general purpose based solution to be available that > > > doesn't count on len() working on an arbitrary iterable. > > [Peter Otten] > > You show signs of a severe case of

Re: Last iteration?

2007-10-18 Thread Paul Hankin
On Oct 17, 11:45 pm, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > [Paul Hankin] > > > def lastdetector(iterable): > > t, u = tee(iterable) > > return izip(chain(imap(lambda x: False, islice(u, 1, None)), > > [True]), t) > > Sweet! Nice, clean piece of iterator algebra. > > We nee

Re: Last iteration?

2007-10-17 Thread Hendrik van Rooyen
"Raymond Hettinger" wrote: > 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) If this is what you call straightforward - heave

Re: Last iteration?

2007-10-17 Thread Paul Rubin
Raymond Hettinger <[EMAIL PROTECTED]> writes: > We need a C-speed verion of the lambda function, something like a K > combinator that consumes arguments and emits constants. Some discussion of this is at . I had suggested implementing K through an optional seco

Re: Last iteration?

2007-10-17 Thread Raymond Hettinger
[Paul Hankin] > def lastdetector(iterable): > t, u = tee(iterable) > return izip(chain(imap(lambda x: False, islice(u, 1, None)), > [True]), t) Sweet! Nice, clean piece of iterator algebra. We need a C-speed verion of the lambda function, something like a K combinator that consum

Re: Last iteration?

2007-10-17 Thread Paul Hankin
On Oct 17, 8:16 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > > def lastdetecter(iterable): > > "fast iterator algebra" > > lookahead, t = tee(iterable) > > lookahead.next() > > t = iter(t) > > return chain(izip(repeat(False), imap(itemgetter(1), > >

Re: Last iteration?

2007-10-17 Thread Peter Otten
Raymond Hettinger wrote: > [Diez B. Roggisch] >> > out:) But I wanted a general purpose based solution to be available that >> > doesn't count on len() working on an arbitrary iterable. > > [Peter Otten] >> You show signs of a severe case of morbus itertools. >> I, too, am affected and have not y

Re: Last iteration?

2007-10-17 Thread Raymond Hettinger
> 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: d

Re: Last iteration?

2007-10-16 Thread Raymond Hettinger
[Diez B. Roggisch] > > out:) But I wanted a general purpose based solution to be available that > > doesn't count on len() working on an arbitrary iterable. [Peter Otten] > You show signs of a severe case of morbus itertools. > I, too, am affected and have not yet fully recovered... Maybe you guy

Re: Last iteration?

2007-10-16 Thread Bruno Desthuilliers
Paul Hankin a écrit : > On Oct 12, 11:58 am, Florian Lindner <[EMAIL PROTECTED]> wrote: > >>Hello, >>can I determine somehow if the iteration on a list of values is the last >>iteration? >> >>Example: >> >>for i in [1, 2, 3]: >> if last_iteration: >> print i*i >> else: >> print i >

Re: Last iteration?

2007-10-15 Thread Peter Otten
Diez B. Roggisch wrote: > out:) But I wanted a general purpose based solution to be available that > doesn't count on len() working on an arbitrary iterable. You show signs of a severe case of morbus itertools. I, too, am affected and have not yet fully recovered... Peter -- http://mail.python

Re: Last iteration?

2007-10-14 Thread Paul McGuire
On Oct 14, 5:58 am, Paul Hankin <[EMAIL PROTECTED]> wrote: > On Oct 14, 8:00 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > > def signal_last(lst): > > last2 = None > > it = iter(lst) > > try: > > last = it.next() > > except StopIteration: > > last = None > > for

Re: Last iteration?

2007-10-14 Thread Diez B. Roggisch
Peter Otten schrieb: > Diez B. Roggisch wrote: > >> Florian Lindner wrote: > >>> can I determine somehow if the iteration on a list of values is the >>> last iteration? > >> def last_iter(iterable): >> it = iter(iterable) >> buffer = [it.next()] >> for i in it: >> buffer.app

Re: Last iteration?

2007-10-14 Thread Paul Hankin
On Oct 14, 8:00 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > On Oct 12, 5:58 am, Florian Lindner <[EMAIL PROTECTED]> wrote: > > > > > Hello, > > can I determine somehow if the iteration on a list of values is the last > > iteration? > > > Example: > > > for i in [1, 2, 3]: > >if last_iteration

Re: Last iteration?

2007-10-14 Thread Paul McGuire
On Oct 12, 5:58 am, Florian Lindner <[EMAIL PROTECTED]> wrote: > Hello, > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 >

Re: Last iteration?

2007-10-13 Thread Robin Kåveland
On Oct 12, 12:58 pm, Florian Lindner <[EMAIL PROTECTED]> wrote: > Hello, > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 >

Re: Last iteration?

2007-10-12 Thread Peter Otten
Diez B. Roggisch wrote: > Florian Lindner wrote: >> can I determine somehow if the iteration on a list of values is the >> last iteration? > def last_iter(iterable): > it = iter(iterable) > buffer = [it.next()] > for i in it: > buffer.append(i) > old, buffer = buffer

Re: Last iteration?

2007-10-12 Thread Paul Hankin
On Oct 12, 2:18 pm, Carsten Haese <[EMAIL PROTECTED]> wrote: > On Fri, 2007-10-12 at 12:58 +0200, Florian Lindner wrote: > > Hello, > > can I determine somehow if the iteration on a list of values is the last > > iteration? > > > Example: > > > for i in [1, 2, 3]: > >if last_iteration: > >

Re: Last iteration?

2007-10-12 Thread Carsten Haese
On Fri, 2007-10-12 at 12:58 +0200, Florian Lindner wrote: > Hello, > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 > 9

Re: Last iteration?

2007-10-12 Thread tasjaevan
On Oct 12, 11:58 am, Florian Lindner <[EMAIL PROTECTED]> wrote: > Hello, > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 >

Re: Last iteration?

2007-10-12 Thread Paul Hankin
On Oct 12, 11:58 am, Florian Lindner <[EMAIL PROTECTED]> wrote: > Hello, > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i Yes, either use enumerate or ju

Re: Last iteration?

2007-10-12 Thread Diez B. Roggisch
Florian Lindner wrote: > Hello, > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 > 9 > > > Can this be acomplished s

Re: Last iteration?

2007-10-12 Thread Stefan Behnel
Florian Lindner wrote: > can I determine somehow if the iteration on a list of values is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 > 9 > > > Can this be acomplished somehow? Y

RE: Last iteration?

2007-10-12 Thread Andreas Tawn
> Hello, > can I determine somehow if the iteration on a list of values > is the last > iteration? > > Example: > > for i in [1, 2, 3]: >if last_iteration: > print i*i >else: > print i > > that would print > > 1 > 2 > 9 Something like: myList = [1, 2, 3] for i, j in enume