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 builtin somewhere that provides better
> functionality that I'm missing?

You could use a generator comprehension (untested):
   
   for prev,cur,next in ((myarray[i-1], myarray[i], myarray[i+1])
                          for i in xrange(1,len(myarray)-1)):
      do_something_with (prev, cur, next)

Alternatively (untested):

   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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to