Slicing wrapped numpy arrays

Martin Manns mmanns at gmx.net
Sun Jan 13 16:31:15 EST 2008


Hi,

I have created a class that wraps a numpy array of custom objects. I
would like to be able to slice respective objects (without copying the
array if possible).

I have browsed the doc and found some hints at __getitem__. However, I
still do not grasp how to do it.  How do I implement __getitem__
correctly?


from numpy import *

class Cell(object):
    pass

class Map(object):
    def __init__(self, dimensions):
        self.generate_map(dimensions)

    def generate_map(self, dimensions):
        map_range = xrange(reduce(lambda x,y: x*y, dimensions))
        self.map = array([Cell() for i in map_range])
        self.map = self.map.reshape(dimensions)

mymap = Map((100, 100, 100))
mymap[10:20,15:20,:] # This line should work afterwards


Thanks in advance

Martin



More information about the Python-list mailing list