生成长度为n的列表,其中包含m个可能的元素

Chr*_*oph 3 python combinations permutation python-itertools

我需要在Python中生成大量的列表.每个列表的长度为13,我有4个可能的值可以进入每个元素.这些是[1,-1,i,-i],但它可以是任何东西.

因此,考虑到主题中的信息,我应该得到4*4*4 ...*4 = 4 ^ 13 = 67,108,864个列表,或者更一般地,m ^ n.

我在Python的itertools中尝试了combination_with_replacement方法,但是使用以下代码我只得到560个结果.

c = it.combinations_with_replacement([1,-1,np.complex(0,1), np.complex(0,-1)], 13)
print list(c)
Run Code Online (Sandbox Code Playgroud)

我知道组合不关心顺序,所以这个结果可能是正确的.但是,当我使用排列方法时,我只能选择第二个参数<=第一个参数中的元素个数.

知道怎么做到这一点?

谢谢!

tom*_*m10 7

我想你想要的

y = itertools.product((1, -1, 1j, -1j), repeat=13)
Run Code Online (Sandbox Code Playgroud)


然后,顺便说一句,print sum(1 for x in y)打印67108864,如你所料.