Scenarios from K number of designs in N number of positions - Python

Viewed 19

I am trying to generate all the possible scenarios which I can place k number of designs in n number of positions. Which should give k^n scenarios. For example lets say k = 2 and n = 3, all the scenarios are:

n1 n2 n3
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2

so there has to be 8 (2^8) scenarios.

I have tried combinations , permutations and combinations_with_replacement from itertools but none of seem to give the solution which I am trying for.

Is there a python function to generate this?

1 Answers

You are looking for a product:

for p in itertools.product(range(1,3),repeat = 3):
    print(p)

(1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 2, 2)
(2, 1, 1)
(2, 1, 2)
(2, 2, 1)
(2, 2, 2)
Related