gpt4 book ai didi

python - 使用多处理搜索列表

转载 作者:太空宇宙 更新时间:2023-11-04 02:34:13 24 4
gpt4 key购买 nike

下面是一个示例代码,它检查列表中是否存在某个项目并将其写入另一个 a 列表:

list_1 = [(0,1),(3,2),(1,5),(0,3),(3,7)]

a = []
for i in list_1:
if i[0]==3:
a.append(i)
break

输出:

[(3, 2)]

当找到第一个满足条件的元素时,循环结束。

假设列表 list_1 非常大,如何在这种情况下应用多处理?

最佳答案

Brake your big list into smaller lists
>>> big= [(0,1),(3,2),(1,5),(0,3),(3,7)]
>>> a=big[0:2]
>>> b=big[2:]
define a function to look for your tuple and return it or return None
>>> def doit(L):
for i in L:
if i[0]==3:
return i
return None
figure out how to create a new process and call it with each of the smaller lists
>>> doit(a)
(3, 2)
>>> doit(b)
(3, 7)
look at the tuples returned that are not None in order and choose the first one

关于python - 使用多处理搜索列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48330458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com