A list with periodic boundary conditions

youhvee at googlemail.com youhvee at googlemail.com
Thu May 21 08:08:39 EDT 2009


Hi,

I'm trying to create a new class of list that has periodic boundary
conditions.

Here's what I have so far:

class wrappedList(list):
    def __getitem__(self, p):
        return list.__getitem__(self, p%len(self))
    def __setitem__(self, p, v):
        list.__setitem__(self, p%len(self), v)

>>>> a=wrappedList(range(10))
>>>> a[1]
1
>>>> a[11]
1

But I would also like to make slices. For instance I would like
>>>> a[8:11]
[8, 9, 0]

I can do it by copying, but I want to return a view to the original
data, and I have no idea where to start. It seems that the slice needs
to retain some knowledge of the list from which it derived. i.e. it
needs to know that it is a slice. Any ideas on how I can extend this
to allow views?

Thanks for any help.

-AM



More information about the Python-list mailing list