简体   繁体   English

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

[英]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.

相关问题 为“ n”个布尔元素生成所有可能的排列,这些布尔元素最多具有“ m”个元素为1或True - Generate all possible permutations for 'n' boolean elements that have at most 'm' elements that are 1 or True 如何获得将长度为 n 的列表划分为 m 个子列表的所有可能组合 - How to get all possible combinations of dividing a list of length n into m sublists 给定一个数字,生成长度为n的X和O的所有可能组合的列表 - Given a number, generate a list of all possible combination of X and O with length n 从给定的单词列表中生成具有“N”长度的所有可能组合(寻找不重复) - Generate all possible combination with “N” length from given words list (Looking for no repeat) 如何以组长度和组内元素的所有可能组合将列表拆分为 n 组? - How to split a list into n groups in all possible combinations of group length and elements within group? 按步长m生成长度为n的iterable滑动窗口 - Generate sliding window of iterable with length n by step m 从长度为n的序列中选择m个等距元素 - Choose m evenly spaced elements from a sequence of length n 如何在没有重复的 m 个插槽中生成 n 个(相似)元素的排列 - How to generate permutations of n (similar) elements in m slots with no duplicates 在 Python 中生成总和为 S 的所有可能的长度 N 列表 - Generate all possible lists of length N that sum to S in Python 从两个不同长度的列表( [2 * n] 和 [2 * m] )到 [ 3 * len(unique(n[0],m[0])) ] 的单个列表的列表理解 - List comprehension from two different length lists ( [2 * n] and [2 * m] ) to one single list of [ 3 * len(unique(n[0],m[0])) ]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM