是否有可能在python中并行化以下代码?我想知道如何使用map和lambda函数转换此代码..
values = (1,2,3,4,5 )
def op(x,y):
return x+y
[(i, j, op(i, j))
for i in values
for j in values
if i is not j]
答案 0 :(得分:2)
检查出来:
from itertools import permutations
values = (1,2,3,4,5 )
[(i, j, i+j) for i, j in permutations(values, 2)]
它在python的stdlib中。
如果你想并行运行,请使用python3:
查看import multiprocessing
from itertools import permutations
values = [1, 2, 3, 4, 5]
l = permutations(values, 2)
def f(x):
return x[0], x[1], x[0] + x[1]
with multiprocessing.Pool(5) as p:
data = p.map(f, l)
答案 1 :(得分:2)
您可以将函数op与多处理和map并行化:
from multiprocessing.dummy import Pool as ThreadPool
from itertools import permutations
pool = ThreadPool(4) # Number of threads
values = (1,2,3,4,5)
aux_val = [(i, j) for i,j in permutations(values,2)]
def op(tupx):
result = (tupx[0], tupx[1], tupx[0] + tupx[1])
return result
results = pool.map(op, aux_val)