python怎么用for循环找出最大值_并行化python中嵌套的for循环以查找最大值
发布日期:2021-06-24 17:08:28 浏览次数:2 分类:技术文章

本文共 932 字,大约阅读时间需要 3 分钟。

这里有一个对你有用的解决方案。我改变了你的很多代码,所以如果你有任何问题请问。在

这远远不是实现这一点的唯一方法,尤其是这不是一个节省内存的解决方案。在

你需要给max_workers设置一个对你有用的东西。通常,计算机中逻辑处理器的数量是一个很好的起点。在from concurrent.futures import ThreadPoolExecutor, Future

from itertools import permutations

from collections import namedtuple, defaultdict

Result = namedtuple('Result', ('value', 'word'))

def new_calculate_similarity(word1, word2):

return Result(

calculate_similarity(global_map[word1], global_map[word2]),

word2)

with ThreadPoolExecutor(max_workers=4) as executer:

futures = defaultdict(list)

for word1, word2 in permutations(unique_words, r=2):

futures[word1].append(

executer.submit(new_calculate_similarity, word1, word2))

for word in futures:

# this will block until all calculations have completed for 'word'

results = map(Future.result, futures[word])

max_result = max(results, key=lambda r: r.value)

print(word, max_result.word, max_result.value,

sep='\t',

file=file_co_occurring)

以下是我使用的库的文档:

转载地址:https://blog.csdn.net/weixin_34007963/article/details/114429579 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java 数组 内存溢出_java - 从字节数组创建列表 - 堆栈内存溢出
下一篇:微信公众号 消息 java_微信公众平台 java开发如何在if中回应用户发来的消息

发表评论

最新留言

很好
[***.229.124.182]2024年04月09日 05时06分02秒