How do I merge two python iterators?

前端 未结 13 1221
花落未央
花落未央 2020-12-06 09:29

I have two iterators, a list and an itertools.count object (i.e. an infinite value generator). I would like to merge these two into a resulting ite

13条回答
  •  眼角桃花
    2020-12-06 10:04

    You can do something that is almost exaclty what @Pramod first suggested.

    def izipmerge(a, b):
      for i, j in itertools.izip(a,b):
        yield i
        yield j
    

    The advantage of this approach is that you won't run out of memory if both a and b are infinite.

提交回复
热议问题