Generating all possible combinations in a range using numpy in python

冷暖自知 提交于 2019-12-23 04:43:10

问题


I am looking for an efficient way to generate all possible combinations within a certain big range using numpy or any faster method. 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).


回答1:


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. 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)


来源:https://stackoverflow.com/questions/49869644/generating-all-possible-combinations-in-a-range-using-numpy-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!