Re: [Tutor] Accessing next and previous items during iteration

2005-12-19 Thread Ed Singleton
On 18/12/05, Kent Johnson [EMAIL PROTECTED] wrote:
 Ed Singleton wrote:
  Is it possible to access the next and previous items during an iteration?

 This just came up on c.l.python. Bengt Richter has a nice generator-based 
 solution.
 http://groups.google.com/group/comp.lang.python/browse_thread/thread/2e4533f108fbf172/90d87c91dac844d3?hl=en#90d87c91dac844d3

That's perfect!

It's a lovely piece of code as well.  Obvious once you've seen it, but
you wouldn't have thought of it before hand.

Thanks

Ed
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-18 Thread Kent Johnson
Ed Singleton wrote:
 Is it possible to access the next and previous items during an iteration?

This just came up on c.l.python. Bengt Richter has a nice generator-based 
solution.
http://groups.google.com/group/comp.lang.python/browse_thread/thread/2e4533f108fbf172/90d87c91dac844d3?hl=en#90d87c91dac844d3

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-16 Thread Kent Johnson
Ed Singleton wrote:
 Is it possible to access the next and previous items during an iteration?

There is nothing built in to support this directly.

 I'm currently using:
 
 prev = 0
 current = 0
 next = 0
 for page in folder:
   prev = current
   current = next
   next = page
   if current:
   if prev:
   #add link to previous page
   #add link to next page
 if current:
   if prev:
   #add link to previous page
   #add link to next page

I think there is a bug here - when the loop exits, prev, current and next will 
all be 
valid with next containing the last page. You have already added the links to 
current, it 
is next that needs a link to the previous page.
 
 But this seems a really awkward way to do it.
 
 I've considered iterating and dumping them all into a list and then
 iterating through the list, but that also seems awkward (having to
 iterate twice).

You don't say what kind of object folder is. If it is a directory listing e.g. 
from 
os.listdir() then it is already a list. In any case you should be able to make 
a list 
without an explicit iteration using
pages = list(folder)
 
 Is there a nice way to do it?

How about this:

pages = list(folder)  # make sure we have a list
for i, page in enumerate(pages):
   if i  0:
 previous = pages[i-1]
 # add link to previous

   if i+1  len(pages):
 next = pages[i+1]
 # add link to next

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-16 Thread Chris or Leslie Smith
| Is it possible to access the next and previous items during an
| iteration? 
| 
| I want to use it to iterate through html files in a folder and add
| links in to the next and previous pages.
|
I just did this :-) What I did was 

1) use the glob module to get a list of the files in the directory (*.htm)
2) sort this using a special sort function (in my case the files were named as 
#_# where # was a number and I wanted to sort according to the first number and 
subsort according to the second)
3) then I just stepped through the list using enumerate and used the index to 
find the next and previous names in the list, e.g.


for i, fil in enumerate(flist):
if i1:
prev = flist[i-1]
#do previous link
if i len(flist):
next = flist[i+1]
#do next link


/c

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-16 Thread Chris or Leslie Smith
| Is it possible to access the next and previous items during an
| iteration? 
| 
| I want to use it to iterate through html files in a folder and add
| links in to the next and previous pages.
|
I just did this :-) What I did was 

1) use the glob module to get a list of the files in the directory (*.htm)
2) sort this using a special sort function (in my case the files were named as 
#_# where # was a number and I wanted to sort according to the first number and 
subsort according to the second)
3) then I just stepped through the list using enumerate and used the index to 
find the next and previous names in the list, e.g.


for i, fil in enumerate(flist):
if i1:
prev = flist[i-1]
#do previous link
if i len(flist):
next = flist[i+1]
#do next link


/c

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-16 Thread Chris or Leslie Smith
| Is it possible to access the next and previous items during an
| iteration? 
| 
| I want to use it to iterate through html files in a folder and add
| links in to the next and previous pages.
|
I just did this :-) What I did was 

1) use the glob module to get a list of the files in the directory (*.htm)
2) sort this using a special sort function (in my case the files were named as 
#_# where # was a number and I wanted to sort according to the first number and 
subsort according to the second)
3) then I just stepped through the list using enumerate and used the index to 
find the next and previous names in the list, e.g.


for i, fil in enumerate(flist):
if i1:
prev = flist[i-1]
#do previous link
if i len(flist):
next = flist[i+1]
#do next link


/c

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-16 Thread Kent Johnson
Chris or Leslie Smith wrote:
 I just did this :-) What I did was 
 
 1) use the glob module to get a list of the files in the directory (*.htm)
 2) sort this using a special sort function (in my case the files were named 
 as #_# where # was a number and I wanted to sort according to the first 
 number and subsort according to the second)
 3) then I just stepped through the list using enumerate and used the index to 
 find the next and previous names in the list, e.g.
 

You have a couple of errors in your conditionals
 
 for i, fil in enumerate(flist):
 if i1:
 prev = flist[i-1]

This should be 'if i = 1'. If i==0 your condition will be true and you will 
set prev = 
flist[-1] which is the *last* item in flist.

 #do previous link
 if i len(flist):
 next = flist[i+1]

should be 'if i+1  len(flist)' to avoid an IndexError on the last element.

Kent

 #do next link
 
 
 /c
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor