Python itertools.product reorder the generation
问题 I have this: 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) So far, so good, itertools.product advances the rightmost element on every iteration. But now I want to be able to specify the iteration order according to the following: axes = (0, 1) # normal order # output: (0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) axes = (1,