Filling a space...

holger krekel pyth at devel.trillke.net
Tue Jun 11 16:34:21 CEST 2002


Thor wrote:
> Having this tupe: (2,3,4), which one would be the msot efficient way to get 
> this (parentheses ommited):
> 
> 0,0,0  0,0,1  0,0,2  0,0,3
> 0,1,0  0,1,1  0,1,2  0,1,3
> 0,2,0  0,2,1  0,2,2  0,2,3
> 1,0,0  1,0,1  1,0,2  1,0,3....
> 
> I mean, to get all the possible integer points in a defined space.

i hope you are going to cope well with all the answers you will certainly get.
i can't resist either to present a >=python2.2 solution :-)

def allpoints_generator(spectuple):
    aslist = list(spectuple)
    if aslist:
        for i in xrange(aslist.pop(0)):
            if aslist:
                for sub in allpoints_generator(aslist):
                     yield (i,)+sub
            else:
                yield (i,)

for i in allpoints_generator( (1,2,3) ):
    print "result", i

result (0, 0, 0)
result (0, 0, 1)
result (0, 0, 2)
result (0, 1, 0)
result (0, 1, 1)
result (0, 1, 2)

have fun,

    holger





More information about the Python-list mailing list