简体   繁体   English

在python中使用numpy生成范围内的所有可能组合

[英]Generating all possible combinations in a range using numpy in python

I am looking for an efficient way to generate all possible combinations within a certain big range using numpy or any faster method. 我正在寻找一种有效的方法来使用numpy或任何更快的方法在一定范围内生成所有可能的组合。 I tried: 我试过了:

from numpy import *
from itertools import *

dt=dtype('i,i,i,i,i,i')
fromiter(combinations(range(10000000),6), dtype=dt, count=-1)

but I get a memory error, and even if it worked it will likely take forever to complete. 但是我遇到了内存错误,即使它可以正常工作,也可能需要永远的时间才能完成。 I am looking for combinations that dont repeat. 我正在寻找不会重复的组合。 For example, if I needed all 3 number combinations in range(1,5) I will get (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4). 例如,如果我需要range(1,5)中的所有3个数字组合,我将得到(1、2、3),(1、2、4),(1、3、4),(2、3、4 )。

There is around 1,000,000,000,000,000,000,000,000,000,000,000,000,000,000 (1 Septillion) possible combinations of 6 elements with the range you are using. 6个元素与您正在使用的范围大约有1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000个(9月1日)可能的组合。 You'll never process them all. 您将永远不会全部处理它们。 The best you can do is to process them in the "lazy way" with a iterator: 最好的办法是使用迭代器以“惰性”方式处理它们:

for c in combinations(range(10000000),6):
    print(c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM