相关疑难解决方法(0)

Python itertools.product重新排序生成

我有这个:

shape = (2, 4) # arbitrary, could be 3 dimensions such as (3, 5, 7), etc...

for i in itertools.product(*(range(x) for x in shape)):
    print(i)

# output: (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3)
Run Code Online (Sandbox Code Playgroud)

到目前为止,非常好,itertools.product在每次迭代中推进最右边的元素.但现在我希望能够根据以下内容指定迭代顺序:

axes = (0, 1) # normal order
# output: (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3)

axes = (1, 0) # reversed order
# …
Run Code Online (Sandbox Code Playgroud)

python python-itertools

9
推荐指数
2
解决办法
2429
查看次数

标签 统计

python ×1

python-itertools ×1