Re: Accessing next/prev element while for looping
- From: Paul Rubin <http://phr.cx@xxxxxxxxxxxxxx>
- Date: 19 Dec 2005 00:35:17 -0800
Paul Rubin <http://phr.cx@xxxxxxxxxxxxxx> writes:
> elts = iter(myarray)
> prev,cur,next = elts.next(),elts.next(),elts.next()
> for next2 in elts:
> do_something_with (prev, cur, next)
> prev,cur,next = cur, next, next2
>
> Of course these fail when there's less than 3 elements.
Ehh, too clever for my own good. This fails with exactly 3 elements.
It was a "shortened" version of
elts = iter(myarray)
prev,cur,next = elts.next(),elts.next(),elts.next()
try:
while True:
do_something_with (prev, cur, next)
prev,cur,next = cur, next, elts.next()
except StopIteration: pass
which is messy, and which could get confused by do_something_with...
raising StopIteration for some reason.
.
- References:
- Accessing next/prev element while for looping
- From: Joseph Garvin
- Re: Accessing next/prev element while for looping
- From: Paul Rubin
- Accessing next/prev element while for looping
- Prev by Date: Re: Accessing next/prev element while for looping
- Next by Date: Testing the availability of a module
- Previous by thread: Re: Accessing next/prev element while for looping
- Next by thread: ANN: eric 3.8.1 released
- Index(es):
Relevant Pages
|