小编abh*_*sok的帖子

任何加快itertool.product速度的方法

我正在使用itertools.product来查找资产的可能权重,因为所有权重之和总计为100。

min_wt = 10
max_wt = 50
step = 10
nb_Assets = 5

weight_mat = []
for i in itertools.product(range(min_wt, (max_wt+1), step), repeat = nb_Assets):
    if sum(i) == 100:
        weight = [i]
        if np.shape(weight_mat)[0] == 0:
            weight_mat = weight
        else:
            weight_mat = np.concatenate((weight_mat, weight), axis = 0)
Run Code Online (Sandbox Code Playgroud)

上面的代码可以工作,但是因为它经历了不可接受的组合,所以它太慢了,示例[50,50,50,50,50]最终测试了3125个组合,而不是121个可能的组合。有什么方法可以在循环中添加“求和”条件以加快处理速度?

python numpy nested-loops python-itertools

6
推荐指数
1
解决办法
83
查看次数

标签 统计

nested-loops ×1

numpy ×1

python ×1

python-itertools ×1