Re: Accessing next/prev element while for looping

2005-12-19 Thread Peter Otten
Bengt Richter wrote: > >>> def pcniter(seq, NULL=NotImplemented): > ... seqiter = iter(seq) > ... prev = curr = NULL > ... try: next = seqiter.next() > ... except StopIteration: return > ... for item in seqiter: > ... prev, curr, next = curr, next, item > ... yi

Re: Accessing next/prev element while for looping

2005-12-19 Thread Paul Rubin
Paul Rubin 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.

Re: Accessing next/prev element while for looping

2005-12-19 Thread Paul Rubin
Joseph Garvin <[EMAIL PROTECTED]> writes: > And this way I can keep referring to j instead of myarray[i], but I'm > still forced to use myarray[i-1] and myarray[i+1] to refer to the > previous and next elements. Being able to do j.prev, j.next seems more > intuitive. > > Is there some other builti

Re: Accessing next/prev element while for looping

2005-12-19 Thread Steve Holden
Steven D'Aprano wrote: > On Sun, 18 Dec 2005 04:50:20 -0800, Max Erickson wrote: > > >>>j is a built-in object used to make complex numbers. Or at least it >>>was, until you rebound it to the current element from myarray. That's bad >>>practice, but since using complex numbers is rather unusual,

Re: Accessing next/prev element while for looping

2005-12-18 Thread Joseph Garvin
Steven D'Aprano wrote: >On Sun, 18 Dec 2005 23:36:29 +1100, Steven D'Aprano wrote: > > > >>Python lists aren't linked lists? They are arrays. >> >> > >[slaps head for the stupid typo] >That should have been a full stop, not question mark. Python lists are not >linked lists, period. > > > >

Re: Accessing next/prev element while for looping

2005-12-18 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > I can't speak for others, but I've never come upon a situation where I > needed to access the element before and the element after the current one. > > [thinks...] Wait, no, there was once, when I was writing a parser that > iterated over lines. I need

Re: Accessing next/prev element while for looping

2005-12-18 Thread Bengt Richter
On Sun, 18 Dec 2005 04:23:21 -0700, Joseph Garvin <[EMAIL PROTECTED]> wrote: >When I first came to Python I did a lot of C style loops like this: > >for i in range(len(myarray)): >print myarray[i] > >Obviously the more pythonic way is: > >for i in my array: >print i > >The python way is mu

Re: Accessing next/prev element while for looping

2005-12-18 Thread Steven D'Aprano
On Sun, 18 Dec 2005 04:50:20 -0800, Max Erickson wrote: >>j is a built-in object used to make complex numbers. Or at least it >>was, until you rebound it to the current element from myarray. That's bad >>practice, but since using complex numbers is rather unusual, one you will >>probably get away

Re: Accessing next/prev element while for looping

2005-12-18 Thread Bas
Just make a custom generator function: >>> def prevcurnext(seq): it = iter(seq) prev = it.next() cur = it.next() for next in it: yield (prev,cur,next) prev,cur = cur, next >>> for (a,b,c) in prevcurnext(range(10)): print a,b

Re: Accessing next/prev element while for looping

2005-12-18 Thread Max Erickson
j> is a built-in object used to make complex numbers. Or at least it >was, until you rebound it to the current element from myarray. That's bad >practice, but since using complex numbers is rather unusual, one you will >probably get away with. Is it? >>> j Traceback (most recent call last): Fi

Re: Accessing next/prev element while for looping

2005-12-18 Thread Steven D'Aprano
On Sun, 18 Dec 2005 23:36:29 +1100, Steven D'Aprano wrote: > Python lists aren't linked lists? They are arrays. [slaps head for the stupid typo] That should have been a full stop, not question mark. Python lists are not linked lists, period. -- Steven. -- http://mail.python.org/mailman/listi

Re: Accessing next/prev element while for looping

2005-12-18 Thread Steven D'Aprano
On Sun, 18 Dec 2005 04:23:21 -0700, Joseph Garvin wrote: > When I first came to Python I did a lot of C style loops like this: > > for i in range(len(myarray)): > print myarray[i] > > Obviously the more pythonic way is: > > for i in my array: > print i > > The python way is much more s

Re: Accessing next/prev element while for looping

2005-12-18 Thread Nick Craig-Wood
Joseph Garvin <[EMAIL PROTECTED]> wrote: > When I first came to Python I did a lot of C style loops like this: > > for i in range(len(myarray)): > print myarray[i] > > Obviously the more pythonic way is: > > for i in my array: > print i > > The python way is much more succinct. B

Accessing next/prev element while for looping

2005-12-18 Thread Joseph Garvin
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray[i] Obviously the more pythonic way is: for i in my array: print i The python way is much more succinct. But a lot of times I'll be looping through something, and if a certain