How do I merge two python iterators?

前端 未结 13 1175
花落未央
花落未央 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:14

    Why is itertools needed?

    def imerge(a,b):
        for i,j in zip(a,b):
            yield i
            yield j
    

    In this case at least one of a or b must be of finite length, cause zip will return a list, not an iterator. If you need an iterator as output then you can go for the Claudiu solution.

    0 讨论(0)
提交回复
热议问题