如何在Python中从多处理返回扁平列表?

如何在Python中从多处理返回扁平列表?,python,multiprocessing,Python,Multiprocessing,我有一段代码 我的工作者返回一个列表,我想要一个主列表,它是所有列表的并集 from multiprocessing import Pool, Manager manager = Manager() another_shared_list = manager.list() def worker2(number): return [x for x in xrange(number)] numbers = [5,7,2,4] pool1 = Pool(4) another_shared_

我有一段代码

我的工作者返回一个列表,我想要一个主列表,它是所有列表的并集

from multiprocessing import Pool, Manager
manager = Manager()
another_shared_list = manager.list()

def worker2(number):
    return [x for x in xrange(number)]

numbers = [5,7,2,4]
pool1 = Pool(4)
another_shared_list.extend(pool1.map(worker2, numbers))
print another_shared_list
它打印

[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1], [0, 1, 2, 3]]
你可能已经猜到了,我想要另一个共享列表

[0,1,2,3,4,0,1,2,3,4,5,6,0,1,0,1,2,3]
我应该怎么做

编辑:
我知道这似乎是一个扁平化的问题,而不是多重处理。但我倾向于避免使用itertools。我希望另一个共享列表可以直接从call pool1.map或其他内容中获取扁平列表

使用
itertools.chain

itertools.chain(*another_shared_list)
工作示例:

another_shared_list = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1], [0, 1, 2, 3]]
import itertools
list(itertools.chain(*another_shared_list))
[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 0, 1, 0, 1, 2, 3]
请注意,
chain
返回一个迭代器,如果需要,必须将其使用到列表中

或如下文所述:

itertools.chain.from_iterable(another_shared_list) #to avoid unpacking

chain.from_iterable()
将避免解包。例如@IljaEverilä您的问题与
多处理模块无关;您已经获得了列表,只需将其展平,如中所示。@alexis我可以直接从pool1.map中的调用中获得展平的列表吗?为什么不可以?它返回一个迭代器,因此您应该能够直接解压缩结果:
另一个共享列表。extend(pool1中的lst为e。lst中的e为map(worker2,number)