我尽量让自己熟悉列表理解。我有以下pythonic循环:
p = multiprocessing.Pool(processes=multiprocessing.cpu_count())
for result in p.imap_unordered(process_next, [(x1, models, y1) for _ in range(iterations)]):
# some stuff
我不确定它是否等同于:
p = multiprocessing.Pool(processes=multiprocessing.cpu_count())
for result in p:
p.imap_unordered(process_next, [(x1, models, y1)])
M = []
for _ in range(iterations):
M.append(_)
请说,有人可以验证我的理解吗?谢谢
答案 0 :(得分:4)
列表理解是调用p.imap_unordered()
的一个参数;你需要先提取它:
_arg2 = [(x1, models, y1) for _ in range(iterations)]
for result in p.imap_unordered(process_next, _arg2):
# ...
现在您可以将其扩展为传统的for
循环:
_arg2 = []
for _ in range(iterations):
_arg2.append((x1, models, y1))
for result in p.imap_unordered(process_next, _arg2):
# ...
for result in p.imap_unordered()
循环和调用独立于列表推导。