如何在python的joblib库中使用嵌套循环 [英] How to use nested loops in joblib library in python

查看:262
本文介绍了如何在python的joblib库中使用嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际代码如下:

def compute_score(row_list,column_list): 

    for i in range(len(row_list)):
            for j in range(len(column_list)):
                tf_score = self.compute_tf(column_list[j],row_list[i])

我想实现多重处理,即在j的每次迭代中我都希望合并column_list.由于compute_tf函数运行缓慢,因此我想对其进行多处理.

I am tying to achieve multi-processing i.e. at every iteration of j I want to pool column_list. Since compute_tf function is slow I want to multi-process it.

我发现必须在Python中使用joblib来做到这一点,但是我无法解决嵌套循环的问题.

I've found have to do it using joblib in Python, But I am unable to workaround with nested loops.

Parallel(n_jobs=2)(delayed(self.compute_tf)<some_way_to_use_nested_loops>)

这是要实现的. 如果提供有关此问题的任何解决方案或任何其他解决方案,那将是一个很大的帮助.

This is what is to be achieved. It would be a great help if any solution on this is provided or any-other solution.

推荐答案

无需实现生成器功能的另一种解决方案是对生成器使用嵌套列表推导:

Another solution without having to implement a generator function, is to use the nested list comprehension for the generator:

Parallel(n_jobs=2)(delayed(self.compute_tf)(i, j) for j in column_list for i in row_list)

顺序将为:

[(i, j) for j in range(10) for i in range(10)]

这篇关于如何在python的joblib库中使用嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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