python:intvectorrange
This is an old revision of the document!
# gives all possible integer vectors between two endpoints, inclusively. For example: # IntVectorRange([0,0,2], [1,1,3]) yields: # [0, 0, 2] # [1, 0, 2] # [0, 1, 2] # [1, 1, 2] # [0, 0, 3] # [1, 0, 3] # [0, 1, 3] # [1, 1, 3] class IntVectorRange: def __init__(self,start,end=None): if end is None: end=start start=[0] * len(end) self.start=start self.end=end self.size = len(start) self.delta = [end[i]-start[i]+1 for i in xrange(self.size)] self.len = reduce(lambda x,y: x*y, self.delta, 1) def __getitem__(self,index): if index >= self.len: raise IndexError r = [0] * self.size for i in xrange(self.size): r[i] = index % self.delta[i] + self.start[i] index /= self.delta[i] return r def __len__(self): return self.len
python/intvectorrange.1274975025.txt.gz · Last modified: 2010/05/27 08:43 by tkbletsc