简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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