[英]Generate a list of length n with m possible elements
I need to generate a ton of lists in Python. 我需要在Python中生成大量的列表。 Every list is of length 13, and I have 4 possible values that can go into each element.
每个列表的长度为13,我有4个可能的值可以进入每个元素。 These are [1, -1, i, -i], but it could be whatever.
这些是[1,-1,i,-i],但它可以是任何东西。
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. 因此,考虑到主题中的信息,我应该得到4 * 4 * 4 ... * 4 = 4 ^ 13 = 67,108,864个列表,或者更一般地,m ^ n。
I tried the combinations_with_replacement method in Python's itertools, but with the following code I only get 560 results. 我在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)
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! 谢谢!
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. 然后,顺便说一句,
print sum(1 for x in y)
打印, 67108864
,如您所料。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.