下面是一个示例代码,它检查列表中是否存在某个项目并将其写入另一个 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
