Iterate/Enumerate Python 中 N^5 的子集

Iterate/Enumerate over a subset of N^5 in Python

我有 ℕ5 的子集,五维向量,其元素位于自然数中。此子集定义为区间 [a1a2 的乘积]×[b1,b2]×...×[f1, f2 ],我想枚举大小为 1 的子集,即向量 (x1, x2,x3,x4, x5) 其中 x1 在 [a1, a2], x2 在 [b1b2] 等。最好的方法是什么?

这是 cartesian product and you can use itertools.product 来计算它:

a = [0, 1]
b = [2, 3]
c = [4, 5]
d = [6, 7]
e = [8, 9]

for subset in product(a, b, c, d, e):
    print(subset)

输出

(0, 2, 4, 6, 8)
(0, 2, 4, 6, 9)
(0, 2, 4, 7, 8)
(0, 2, 4, 7, 9)
(0, 2, 5, 6, 8)
(0, 2, 5, 6, 9)
(0, 2, 5, 7, 8)
(0, 2, 5, 7, 9)
(0, 3, 4, 6, 8)
(0, 3, 4, 6, 9)
(0, 3, 4, 7, 8)
(0, 3, 4, 7, 9)
(0, 3, 5, 6, 8)
(0, 3, 5, 6, 9)
(0, 3, 5, 7, 8)
(0, 3, 5, 7, 9)
(1, 2, 4, 6, 8)
(1, 2, 4, 6, 9)
(1, 2, 4, 7, 8)
(1, 2, 4, 7, 9)
(1, 2, 5, 6, 8)
(1, 2, 5, 6, 9)
(1, 2, 5, 7, 8)
(1, 2, 5, 7, 9)
(1, 3, 4, 6, 8)
(1, 3, 4, 6, 9)
(1, 3, 4, 7, 8)
(1, 3, 4, 7, 9)
(1, 3, 5, 6, 8)
(1, 3, 5, 6, 9)
(1, 3, 5, 7, 8)
(1, 3, 5, 7, 9)