Python:如何并行化比较两个列表的作业?

2024-09-29 21:41:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个列表,需要比较和计算元素的元素。随着这些列表越来越大,性能也越来越差。有人建议将其中一个列表分成N个部分,并并行运行比较。如何并行运行这些程序

key={}
#compare each list, element by element
for i in range(len(list1)):
    for j in range(len(list2)):
        matched = False
        try:
            matched = match_function(list[i]['typeforma'], list[i]['typeformb'],list[j]['typeforma'], list[j]['typeformb'], ) 
        except:
            print("Error",i,j)
        if matched:
            # store two matches in the dictionary
            key[list2[j]['id']]=list1[i]['identifier']
            break;
        j+=1
    i+=1

Tags: keyin元素列表forlenrangeelement
1条回答
网友
1楼 · 发布于 2024-09-29 21:41:42

假设您确实需要比较笛卡尔积(list1中的每个元素与list2中的每个元素,而不是只比较list1中的每个元素与list2中相应的元素),最简单的方法就是用对^{}^{}map调用替换外循环

唯一的诀窍是你不想尝试共享可变的keydict;相反,传回单独的dict并在末尾合并

例如:

def compare_to_list2(i):
    key = {}
    for j in range(len(list2)):
        matched = False
        try:
            matched = match_function(list[i]['typeforma'], list[i]['typeformb'],list[j]['typeforma'], list[j]['typeformb'], ) 
        except:
            print("Error",i,j)
        if matched:
            # store two matches in the dictionary
            key[list2[j]['id']]=list1[i]['identifier']
            break;
        j+=1
    return key

with concurrent.futures.ProcessPoolExecutor as x:
    key = {}
    for result in x.map(compare_to_list2, range(len(list1)), chunksize=1024):
        key.update(result)

使用chunksize进行实验,但首先,有一些方法可以改进这一点。仅举一个例子,您应该直接在list1list2上迭代,而不是在range(len(list1))range(len(list2))上迭代—这样做不仅会使事情更简单,而且效率更高,特别是对于大的块大小。事实上,最好先简化,然后再优化

相关问题 更多 >

    热门问题