我有如下的 python 代码:
import numpy as np
import multiprocessing as mp
def func(first, sec):
results = []
for x in range(first):
result_dict = {"a": x, "b": x**sec, "c": x/sec}
results.append(result_dict.copy())
return results
with mp.Pool(mp.cpu_count()) as pool:
res = pool.starmap(func, [(a, 8) for a in range(1, 4)])
然后我res用这段代码展平了:
res = np.asarray(res)
res = res.reshape(-1)
res = np.array(res).tolist()
之后,当我打印res输出时,如下所示:
[[{'a': 0, 'b': 0, 'c': 0.0}],
[{'a': 0, 'b': 0, 'c': 0.0}, {'a': 1, 'b': 1, 'c': 0.125}],
[{'a': 0, 'b': 0, 'c': 0.0},
{'a': 1, 'b': 1, 'c': 0.125},
{'a': 2, 'b': 256, 'c': 0.25}]]
但我希望输出是这样的:
[{'a': 0, 'b': 0, 'c': 0.0},
{'a': 0, 'b': 0, 'c': 0.0},
{'a': 1, 'b': 1, 'c': 0.125},
{'a': 0, 'b': 0, 'c': 0.0},
{'a': 1, 'b': 1, 'c': 0.125},
{'a': 2, 'b': 256, 'c': 0.25}]
您知道如何更改代码以获得所需的结果吗res?
泛舟湖上清波郎朗
杨魅力
相关分类