Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a matrix of options using itertools

I am trying to produce a matrix of True and False values, which shows all the permutations for a given number of choices. So for 5 choices you would have the following output.

F F F F F
T F F F F
T T F F F
T T T F F
...
F T F F F
...

I have been looking at using itertool's permutations and combinations, but these work off position and not value which results in duplicates.

I'm sure there is a standard algorithm for this problem, but I'm struggling to find its name.

like image 300
pledge Avatar asked Aug 12 '11 20:08

pledge


1 Answers

Use itertools.product:

itertools.product([False,True],repeat=5)

example of itertools.product([False,True],repeat=2):

(False, False)
(False, True)
(True, False)
(True, True)
like image 194
Fredrik Pihl Avatar answered Sep 22 '22 15:09

Fredrik Pihl