This can be done with itertools.product()
and a list comprehension:
>>> list(itertools.product(*[range(1, j+1) for j in c]))
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2), (1, 3, 1), (1, 3, 2), (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2), (2, 3, 1), (2, 3, 2)]
We loop through your list, and create a range representing the potential values for each column.
>>> [range(1, j+1) for j in c]
[range(1, 3), range(1, 4), range(1, 3)]
(I use 3.x, and range()
gives a generator, in 2.x, it'll be a list, e.g: [[1, 2], [1, 2, 3], [1, 2]]
, either way it works - as you consume it here anyway, it isn't worth using xrange()
in 2.x unless you particularly feel like it).
Then we unpack this into itertools.product()
which gives us all the possible combinations, which is what you want.