在python中使用numpy生成范围内的所有可能组合 [英] Generating all possible combinations in a range using numpy in python

查看:229
本文介绍了在python中使用numpy生成范围内的所有可能组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种有效的方法,使用numpy或任何更快的方法在一定范围内生成所有可能的组合。我试过了:

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)

,但是出现内存错误,即使它起作用了,也可能要花很长时间才能完成。我正在寻找不会重复的组合。例如,如果我需要range(1,5)中的所有3个数字组合,我将得到(1、2、3),(1、2、4),(1、3、4),(2、3、4 )。

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,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
(9月1日)可能是6个元素与您使用的范围的组合。您将永远不会全部处理它们。最好的办法是使用迭代器以惰性方式处理它们:

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)

这篇关于在python中使用numpy生成范围内的所有可能组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆