如何并行化嵌套列表中 2 个元素的乘法
How to parallelize multiplication of 2 elements in a nested list
如何并行化嵌套列表中 2 个元素的乘法。即如果我的列表是:
lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
我想要得到的输出是:
[1, 2, 3, 2, 4, 6, 3, 6, 9]
其中 1*1=1、1*2=2、1*3=3、2*1=2 等等。
我有以下代码:
from itertools import product
from joblib import Parallel, delayed
lst = (list(a) for a in product([1, 2, 3], repeat=2))
results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
results
但是这段代码给我一个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-68b5134d22bd> in <module>
----> 1 results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
2 results
~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
964 if hasattr(self._backend, 'start_call'):
965 self._backend.start_call()
--> 966 iterator = iter(iterable)
967 pre_dispatch = self.pre_dispatch
968
TypeError: 'function' object is not iterable
1) 我哪里做错了?
2) 我该如何解决这个问题?
这是你需要做的:
from concurrent.futures import ProcessPoolExecutor
lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
def multiply(numbers):
return numbers[0] * numbers[1]
with ProcessPoolExecutor() as pool:
results = [result
for result
in pool.map(multiply, lst)]
print(results)
# [1, 2, 3, 2, 4, 6, 3, 6, 9]
如何并行化嵌套列表中 2 个元素的乘法。即如果我的列表是:
lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
我想要得到的输出是:
[1, 2, 3, 2, 4, 6, 3, 6, 9]
其中 1*1=1、1*2=2、1*3=3、2*1=2 等等。
我有以下代码:
from itertools import product
from joblib import Parallel, delayed
lst = (list(a) for a in product([1, 2, 3], repeat=2))
results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
results
但是这段代码给我一个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-68b5134d22bd> in <module>
----> 1 results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
2 results
~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
964 if hasattr(self._backend, 'start_call'):
965 self._backend.start_call()
--> 966 iterator = iter(iterable)
967 pre_dispatch = self.pre_dispatch
968
TypeError: 'function' object is not iterable
1) 我哪里做错了?
2) 我该如何解决这个问题?
这是你需要做的:
from concurrent.futures import ProcessPoolExecutor
lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
def multiply(numbers):
return numbers[0] * numbers[1]
with ProcessPoolExecutor() as pool:
results = [result
for result
in pool.map(multiply, lst)]
print(results)
# [1, 2, 3, 2, 4, 6, 3, 6, 9]