I have two integers n
and m
. I need to generate all possible combinations but with this order:
For n = 3 and m = 4,
combinations = [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 1, 3], [0, 2, 0], [0, 2, 1], [0, 2, 2], [0, 2, 3], [0, 3, 0], [0, 3, 1], ...]
I can do this as follow:
combinations = []
for i in [0, 1, 2, 3]:
for j in [0, 1, 2, 3]:
for k in [0, 1, 2, 3]:
combinations.append([i, j, k])
I have three for loops because n = 3
and I have [0, 1, 2, 3]
in each loop because m = 4
.
For more general values of m
and n
, how can do it?
combinations = []
for i in range(m):
for j in range(m):
for k in range(m):
for l in range(m):
... # n loops :(
combinations.append([i, j, k, l, ...])