0

I want to define an array with a given number of columns (let's say n=5) and in each cell of the array, the value can be either 0 or 1. And I would like to create all possibilities of ones and zeros, which means, that each row would represent one possible vector with n elements.

In other words, I want the table to look like this:

enter image description here

I know that create the vector of ones and zeros is quite easy but how can I ensure that the vectors would not repeat in the table and that there will be all possible combinations included (If my math is correct the table should have 2**5 = 32 rows)

How can I do it in Python? Thank you very much

Mechatrnk
  • 101
  • 1
  • 13
  • Does this answer your question? [How to get all possible combinations of a list’s elements?](https://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements) – Alexandre B. May 04 '20 at 12:35
  • 1
    What you're looking for is the binary representation of all integers up to `32`, in list form. Using the answers [here](https://stackoverflow.com/q/30971079/4316405), you should be able to roll this yourself. – Nelewout May 04 '20 at 12:38

2 Answers2

1

Easy with itertools:

itertools.product(*[[0, 1]] * 3)

results in

[(0, 0, 0),
 (0, 0, 1),
 (0, 1, 0),
 (0, 1, 1),
 (1, 0, 0),
 (1, 0, 1),
 (1, 1, 0),
 (1, 1, 1)]
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • Thank you, I like the simplicity of this solutions. I apologize for maybe a "dumb" question, when I either try to print(itertools.product(*[[0, 1]] * 3)) or save it as a variable first, the result is just that it is"itertools.product object at some space in memory), what am I doing wrong? Thank you – Mechatrnk May 04 '20 at 12:42
  • Sorry, I added the comment before I finished it, the question I considered "dumb" :) is the following in the edited comment :) – Mechatrnk May 04 '20 at 12:44
  • 1
    @Mechatrnk no worries, no question is dumb :) that thing is called an "iterator", it saves memory by not instantiating all items at once, but generating the next item in the sequence as soon as you need it. (Read more [here](https://www.datacamp.com/community/tutorials/python-iterator-tutorial)). You can get all the elements by wrapping it in `list(itertooos.product(...))` – BlackBear May 04 '20 at 12:46
  • @Mechatrnk if my answer satisfies you, please [accept it](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Thank you! – BlackBear May 04 '20 at 13:48
  • Sorry, the answer cannot be accepted for 10 minutes after asking a question and your answer came before the threshold and I forgot to accept it later :) – Mechatrnk May 04 '20 at 13:55
1

You could generate all the numbers up to 32, and convert each to binary representation using bit shifts.

combs = [[(n >> p) & 1 for p in range(4, -1, -1)] for n in range(32)]

which gives combs as:

[
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1],
 [0, 0, 0, 1, 0],
 [0, 0, 0, 1, 1],
 [0, 0, 1, 0, 0],
 [0, 0, 1, 0, 1],
 [0, 0, 1, 1, 0],
 [0, 0, 1, 1, 1],
 [0, 1, 0, 0, 0],
 [0, 1, 0, 0, 1],
 [0, 1, 0, 1, 0],
 [0, 1, 0, 1, 1],
 [0, 1, 1, 0, 0],
 [0, 1, 1, 0, 1],
 [0, 1, 1, 1, 0],
 [0, 1, 1, 1, 1],
 [1, 0, 0, 0, 0],
 [1, 0, 0, 0, 1],
 [1, 0, 0, 1, 0],
 [1, 0, 0, 1, 1],
 [1, 0, 1, 0, 0],
 [1, 0, 1, 0, 1],
 [1, 0, 1, 1, 0],
 [1, 0, 1, 1, 1],
 [1, 1, 0, 0, 0],
 [1, 1, 0, 0, 1],
 [1, 1, 0, 1, 0],
 [1, 1, 0, 1, 1],
 [1, 1, 1, 0, 0],
 [1, 1, 1, 0, 1],
 [1, 1, 1, 1, 0],
 [1, 1, 1, 1, 1]
]

Alternatively, you could use a recursive generation function:

def gimme_combs(n):
    if n == 1: return [[0], [1]]
    lower_combs = gimme_combs(n - 1)
    return [[0] + c for c in lower_combs] + \
           [[1] + c for c in lower_combs]

which would give the same result when called with:

combs = gimme_combs(5)
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54