You can use the nested class and overload the __getitem__ attribute of your objects:
import numpy as np class indexer: def __init__(self,arr): self.arr=arr self.d=self.caldict(self.arr) self.vals=self.values(self.arr,self.d) self.cols=self.columns(self.d) def caldict(self,arr,dd={}): inds=np.array(np.nonzero(arr)).T for i,j in inds: dd.setdefault(i,[]).append(j) return dd class values: def __init__(self,arr,d): self.arr=arr self.d=d def __getitem__(self,index): try: return self.arr.take(index,axis=0)[self.d[index]] except KeyError: return [] class columns: def __init__(self,d): self.d=d self.c=np.array([ 10, 20, 30, 40]) def __getitem__(self,index): try: return self.c.take(self.d[index]) except KeyError: return []
Demo:
m=np.array([[4, 0, 9, 0], [0, 7, 0, 0], [0, 0, 0, 0], [0, 0, 0, 5]]) o=indexer(m) print o.vals[0],'\n',o.vals[1],'\n',o.vals[2],'\n',o.vals[3] print '------------------' print o.cols[0],'\n',o.cols[1],'\n',o.cols[2],'\n',o.cols[3] [4 9] [7] [] [5] ------------------ [10 30] [20] [] [40]