This question already has answers here:
Why can't I iterate twice over the same data?
                                
                                    (3个答案)
                                
                        
                                去年关闭。
            
                    
所以我有下面的代码工作正常:

from  concurrent.futures import ProcessPoolExecutor
import itertools

def grid_search_helper(vec_input):
    v1 = vec_input[0]
    v2 = vec_input[1]
    v3 = vec_input[2]
    d = {'v1' : v1, 'v2' : v2, 'v3' : v3}
    return(d)

idx = range(0,10)
cutoff = np.ndarray.tolist(np.arange(0.6,0.95,0.05))
opt = [2]

iters = itertools.product(idx, cutoff, opt)

with ProcessPoolExecutor(max_workers = 11) as executor:
        for  res in executor.map(grid_search_helper,iters):
            print(res)


然后,我尝试使用zip()打印出ProcessPoolExecuter正在处理的可迭代项,但是当我运行以下代码时,什么也没打印出来:

from  concurrent.futures import ProcessPoolExecutor
import itertools

def grid_search_helper(vec_input):
    v1 = vec_input[0]
    v2 = vec_input[1]
    v3 = vec_input[2]
    d = {'v1' : v1, 'v2' : v2, 'v3' : v3}
    return(d)

idx = range(0,10)
cutoff = np.ndarray.tolist(np.arange(0.6,0.95,0.05))
opt = [2]

iters = itertools.product(idx, cutoff, opt)

with ProcessPoolExecutor(max_workers = 11) as executor:
        for  res, itr in zip(executor.map(grid_search_helper,iters), iters):
            print(res, itr)


我不知道为什么。有人可以帮忙吗?

最佳答案

这与压缩函数和迭代器无关。

问题是您使用相同的迭代器两次:

#                                                      v       v
for res, itr in zip(executor.map(grid_search_helper, iters), iters):
    ...


第一次传递给map时,将使用它。到再次将其传递给zip时,它已经为空,因此zip返回一个空的生成器,没有任何要迭代的内容。

使用itertools.tee创建同一迭代器的两个副本。

it1, it2 = itertools.tee(itertools.product(idx, cutoff, opt))

with ProcessPoolExecutor(max_workers = 11) as executor:
    for  res, itr in zip(executor.map(grid_search_helper,it1), it2):
        print(res, itr)

关于python - 为什么压缩函数和可迭代的结果不起作用? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51991188/

10-17 01:45