Python - n维立方体的角坐标

fit*_*gle 3 python python-itertools hypercube

我试图从每个维度的分钟和最大值列表中获取n维立方体的坐标.我能够使用for循环获得角落,但我想推广任意数量的维度.

例如:

mins = [-1,-2,-3]
maxes = [1,2,3]
Run Code Online (Sandbox Code Playgroud)

会给出坐标:

(-1, -2, -3), (-1, -2, 3), (-1, 2, -3), (-1, 2, 3),
(1, 2, 3), (1, 2, -3), (1, -2, 3), (1, -2, -3)
Run Code Online (Sandbox Code Playgroud)

这基本上是通过两个列表找到所有路径,从每个索引的一个列表中选择一个值.我已经看到算法给出路径数或最快路径,但我没有找到一个枚举所有可能路径的算法.

我假设itertools会进入解决方案,但无法弄清楚如何使用产品,排列和组合来提供所需的结果.最接近的是:

list(itertools.product((xmin, xmax), (ymin, ymax), (zmin, zmax)))
Run Code Online (Sandbox Code Playgroud)

Joc*_*zel 6

你非常接近,*zip( ... )正是你在寻找:

>>> 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)]
Run Code Online (Sandbox Code Playgroud)