开发者

Combining generators

I have a function that returns a list via yield. I use this function as follows:

myList = []
for i in range(10):
    myList = myList + list(myListGenerator(i))

pickleFile = open("mystuff.dat", "wb")
pickle.dump(myList, pickleFile)
pickleFile.close() 

I'm just wondering if this is the most efficient way to pickle the data or if I can combine the generators (m开发者_Python百科yListGenerator(0), myListGenerator(1), etc) into one generator which can then be used by pickle.

Sorry if my question sonds daft but I'm new to both generators and pickle... Thanks,

Barry


You can combine the results of the generators (created using a generator expression) into a single list with itertools.chain.from_iterable:

pickle.dump(list(itertools.chain.from_iterable(
              myListGenerator(i) for i in range(10))), pickleFile)

Or rewrite the generator to include the range call internally, and then just do

pickle.dump(list(myListGenerator(10)), pickleFile)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜