fromconcurrent.futuresimportThreadPoolExecutor,Futurefromitertoolsimportpermutationsfromcollectionsimportnamedtuple,defaultdictResult=namedtuple('Result',('value','word'))defnew_calculate_similarity(word1,word2):returnResult(calculate_similarity(global_map[word1],global_map[word2]),word2)withThreadPoolExecutor(max_workers=4)asexecuter:futures=defaultdict(list)forword1,word2inpermutations(unique_words,r=2):futures[word1].append(executer.submit(new_calculate_similarity,word1,word2))forwordinfutures:# this will block until all calculations have completed for 'word'results=map(Future.result,futures[word])max_result=max(results,key=lambdar:r.value)print(word,max_result.word,max_result.value,sep='\t',file=file_co_occurring)
python怎么用for循环找出最大值_并行化python中的嵌套for循环以查找最大值
最新推荐文章于 2023-06-28 22:06:36 发布