Create a list of length n with m possible elements

I need to create a ton of lists in Python. Each list has a length of 13, and I have 4 possible values ​​that can go into each item. It is [1, -1, i, -i], but it can be anything.

Thus, I should get 4 * 4 * 4 ... * 4 = 4 ^ 13 = 67,108,864 lists or, more broadly, m ^ n, given the information in the topic.

I tried the combination_with_replacement method in Python itertools, but with the following code, I get only 560 results.

c = it.combinations_with_replacement([1,-1,np.complex(0,1), np.complex(0,-1)], 13)
print list(c)

I know that combinations don't care about order, so this result is probably right. However, when I use the permutation method instead, I can only select the second argument <= the number of elements in the first argument.

Any idea how to do this?

Thank!

+3
source share
1 answer

I think you want

y = itertools.product((1, -1, 1j, -1j), repeat=13)


Then, btw, print sum(1 for x in y)prints 67108864, as you expect.

+7
source

Source: https://habr.com/ru/post/1767986/


All Articles