Python - corner coordinates of n-dimensional cube

2.8k views Asked by At

I'm trying to get the coordinates of an n-dimensional cube from a list of the mins and maxes for each dimension. I'm able to get the corners using for loops but I would like to generalize for any number of dimensions.

So for instance:

mins = [-1,-2,-3]
maxes = [1,2,3]

would give the coordinates:

(-1, -2, -3), (-1, -2, 3), (-1, 2, -3), (-1, 2, 3),
(1, 2, 3), (1, 2, -3), (1, -2, 3), (1, -2, -3)

This is essentially finding all the paths through two lists, choosing a value from one of the lists for each index. I've seen algorithms to give the number of paths or the fastest path, but I haven't found one that enumerates all of the possible paths.

I'd assume itertools would come into the solution, but cannot figure out how to use products, permutations, and combinations in way that gives the desired result. The closest has been:

list(itertools.product((xmin, xmax), (ymin, ymax), (zmin, zmax)))
1

There are 1 answers

2
Jochen Ritzel On

You were pretty close, *zip( ... ) is what you were looking for:

>>> list(itertools.product(*zip([-1,-2,-3],[1,2,3])))
[(-1, -2, -3), (-1, -2, 3), (-1, 2, -3), (-1, 2, 3), (1, -2, -3), (1, -2, 3), (
, 2, -3), (1, 2, 3)]