如何在python的joblib库中使用嵌套循环 [英] How to use nested loops in joblib library in python
问题描述
实际代码如下:
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屋!