简体   繁体   中英

Python - corner coordinates of n-dimensional cube

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)))

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM