views:

101

answers:

1

Hi there,

I need to generate a ton of lists in Python. Every list is of length 13, and I have 4 possible values that can go into each element. These are [1, -1, i, -i], but it could be whatever.

Thus I should get 4 * 4 * 4 ... * 4 = 4^13 = 67,108,864 lists, or more generally, m^n, given the info in the subject.

I tried the combinations_with_replacement method in Python's itertools, but with the following code I only get 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 do not care about order, so this result is probably right. However, when I use the permutations method instead, I can only pick the second argument <= the number of elements in the first argument.

Any idea how to accomplish this?

Thanks!

+7  A: 

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.

tom10
That's it, thanks a lot!
Christoph