473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Accessing next/prev element while for looping

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 condition is met, need to access the
previous or the next element in the array before continuing iterating. I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.

I could also enumerate:

for i, j in enumerate(myarray):
print myarray[i], j # Prints each element twice

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?
Dec 18 '05 #1
13 14684
Joseph Garvin <k0*****@kzoo.edu> 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. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating. I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.
I think you'll find python lists are actually arrays not linked
lists...
I could also enumerate:

for i, j in enumerate(myarray):
print myarray[i], j # Prints each element twice

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.
Boundary conditions are a perenial problem in this sort of thing, what
to do at the start / end of the list...
Is there some other builtin somewhere that provides better functionality
that I'm missing?


I suppose you could use itertools...
from itertools import *
L=range(10)
(L1, L2, L3) = tee(L, 3)
L2.next() 0 L3.next() 0 L3.next() 1 for prev, current, next in izip(L1, L2, L3): print prev, current, next ....
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9


--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Dec 18 '05 #2
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 succinct. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating.
If you need to do that, then either you need to rethink your algorithm so
that you only need to access one element at a time, or you can to change
your loop so that you aren't accessing one element at a time.

For example, you might grab elements three at a time, and if the middle
one meets a certain condition process them, and if not, do nothing.

There is no shortage of ways to handle your situation. However, I can't
help but feel that you probably need to rethink what you are trying to do.
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 needed line continuations, so if the line ended
with a backslash, I needed to access the next line (and potentially the
line after that, and so forth indefinitely). I dealt with that by keeping
a cache of lines seen, adding to the cache if the line ended with a
backslash.
I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.
Python lists aren't linked lists? They are arrays.

I could also enumerate:

for i, j in enumerate(myarray):
print myarray[i], j # Prints each element twice
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.

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.


But that can't work, because j has no knowledge of the list it is part of:

myarray = ["spam", "parrot", "Spanish Inquisition"]
when i = 1, j = "parrot"

Since j is a string, it has no prev or next methods. How could "parrot"
possibly know that "spam" was the previous element?

You can't even have prev and next methods of the list myarray, because it
has no concept of the current element. The current element i is known by
the iterator used by the for loop, not the list it is iterating over.

You could possibly sub-class list, turning it into an iterator-like object
with next and prev methods, but be aware that "next" method has special
meaning to iterators.

You can probably create a generator or iterator that will do what you
want, but I suspect it won't be exactly straightforward.
--
Steven.

Dec 18 '05 #3
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.

Dec 18 '05 #4
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):
File "<pyshell#0>", line 1, in -toplevel-
j
NameError: name 'j' is not defined 1 + 2j (1+2j) j=4
1 + 2j (1+2j)


I am actually curious, as it doesn't appear to be, but you are usually
'right' when you say such things...

Max

Dec 18 '05 #5
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,c
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9

Cheers,
Bas

Dec 18 '05 #6
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 with.


Is it?


[snip example]

Well well, I'll be a monkey's uncle. You learn something new everyday.

Okay, I was wrong. There is no conflict between using j as a name and the
built-in j-as-syntax-for-complex-numbers, except for any potential
conflict in the programmers head.

That makes me feel a lot less guilty about all the times I wrote for j in
range loops :-)
--
Steven.

Dec 18 '05 #7
On Sun, 18 Dec 2005 04:23:21 -0700, Joseph Garvin <k0*****@kzoo.edu> 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. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating. I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.

I could also enumerate:

for i, j in enumerate(myarray):
print myarray[i], j # Prints each element twice

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?

I don't know of a builtin, but you could make an iterator that gives you
your sequence as (prev, curr, next) item tuples, where curr is the normal
single item in for curr in my_array: ... e.g.,
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
... yield prev, curr, next
... yield curr, next, NULL
... for prev, curr, next in pcniter('abcdef', '\x00'): print '%8r'*3 %( prev, curr, next) ...
'\x00' 'a' 'b'
'a' 'b' 'c'
'b' 'c' 'd'
'c' 'd' 'e'
'd' 'e' 'f'
'e' 'f' '\x00' for prev, curr, next in pcniter(xrange(4), 'NULL' ): print '%8r'*3 %( prev, curr, next)

...
'NULL' 0 1
0 1 2
1 2 3
2 3 'NULL'

If you want to assign back into myarray[i-1] etc, you'd still need to enumerate, but you could
combine with the above it was useful.

Regards,
Bengt Richter
Dec 18 '05 #8
Steven D'Aprano <st***@REMOVETHIScyber.com.au> 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 needed line continuations, so if the line ended
with a backslash, I needed to access the next line (and potentially the
line after that, and so forth indefinitely). I dealt with that by keeping
a cache of lines seen, adding to the cache if the line ended with a
backslash.


In Python, that would be an excellent example use case for a generator
able to "bunch up" input items into output items, e.g.:

def bunch_up(seq):
cache = []
for item in seq:
if item.endswith('\\\n'):
cache.append(item[:-2])
else:
yield ''.join(cache) + item
cache = []

if cache:
raise ValueError("extra continuations at end of sequence")

[[or whatever you wish to do instead of raising if the input sequence
anomalously ends with a ``to be continued'' line]].
Alex
Dec 18 '05 #9
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.

All the more inexcusable then, because it's even easier to find the
next/previous element in an array! ;)
Thanks all, Bas/Bangt's solutions are very elegant =)

Dec 19 '05 #10
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, one you will
probably get away with.


Is it?

[snip example]

Well well, I'll be a monkey's uncle. You learn something new everyday.

Okay, I was wrong. There is no conflict between using j as a name and the
built-in j-as-syntax-for-complex-numbers, except for any potential
conflict in the programmers head.

That makes me feel a lot less guilty about all the times I wrote for j in
range loops :-)

Yup, the "j" notation is recognised by the lexer, there's no built-in
object.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Dec 19 '05 #11
Joseph Garvin <k0*****@kzoo.edu> 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.
Dec 19 '05 #12
Paul Rubin <http://ph****@NOSPAM.invalid> 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.
Dec 19 '05 #13
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
...*********yield*prev,*curr,*next
...*****yield*curr,*next,*NULL
...

Just a reminder:

"""
In a generator function, the return statement is not allowed to include
an expression_list[3]. In that context, a bare return indicates that the
generator is done and will cause StopIteration to be raised.
"""

try:
# may raise StopIteration
except StopIteration:
return

is the same as

try:
# may raise StopIteration
except StopIteration:
raise StopIteration

and may be simplified to

# may raise StopIteration

Peter
Dec 19 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Rob Meade | last post by:
Hi all, I went on an XML course over a year ago and have finally got around to having a use for it, but do you think I can actually remember anything - nope!.. Ok, here's what I want to do -...
3
by: gordon.lear | last post by:
I am using MSXML 4.0 I am grabbing numerous RSS News Feeds and trying to parse the data and read it into a dB. The problem is that the RSS News feeds are not all the same when tunneled down in...
1
by: saturnius | last post by:
Hi, I would like to have a navigation menu with prev/next in a top frame and in the main frame a PDF file. I think this might be possible with JavaScript: - get current file name - go to next...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
6
by: Lars Eighner | last post by:
I suppose this is a meta-html question, but it seems to go directly to an authoring issue. Which order of next, home (menu, index, main, contents), prev links do you prefer, and why? 1. next...
6
by: Patrick Coghlan | last post by:
I want to create about 4 forms with the same dimensions and background colours, similar to the forms one has to traverse when installing various software packages. I'm using Visual Studio and...
1
by: jenson | last post by:
<?php /* * Created on Apr 13, 2007 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ require_once 'init.php'; ...
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
16
by: pupilstuff | last post by:
hi guys i just want to perform custom paging in which at the footer of the grid view ,there must be a pager 'pervious/next with numeric' this is what i did in aspx page <asp:GridView...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.