How to generate (enumerate) 2**N tuples representing all vertices of unit hypercube in N-dimensional hyperspace ?

Heiko Wundram modelnine at
Tue Jan 3 20:51:46 EST 2006

Paul Rubin wrote:
> def perm(n):
>    return [tuple([(1,-1)[(t>>i)%2] for i in xrange(n)])
>               for t in xrange(2L**n)]

or replace that with:

def perm(n):
   return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
           for t in xrange(2L**n))

to get a generator like in Paul's first example. Only works with Python 2.4+

--- Heiko.

More information about the Python-list mailing list