Why I think range is a wart.

John Roth johnroth at ameritech.net
Wed Mar 13 20:13:50 EST 2002


"Daniel Dittmar" <daniel.dittmar at sap.com> wrote in message
news:a6o24g$5t5$1 at news1.wdf.sap-ag.de...
> > It happens to me too, mostly when I'm using a sliding window
> > type algorithm, that is, I need to look at two adjacent members
> > of the list. You can't do this easily by iterating through the list;
> > unrolling the first entry and the last entry are a pain.
> >
> > Is there a way of handling this nicely? Somehow I doubt it.
>
> class ListWindow:
>     def __init__ (self, list):
>         self.list = list + [None]   # assuming None is a suitable out
of
> bounds value
>
>     def __getitem__ (self, index):
>         return self.list [index:index + 2]
>
> for currentItem, nextItem in ListWindow (mylist):
>     <whatever>
>
> Daniel

Nice solution. Thanks.

John Roth
>
>





More information about the Python-list mailing list