How do I merge two python iterators?

前端 未结 13 1271
花落未央
花落未央 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 09:50

    Use izip and chain together:

    >>> list(itertools.chain.from_iterable(itertools.izip(items, c))) # 2.6 only
    ['foo', 1, 'bar', 2]
    
    >>> list(itertools.chain(*itertools.izip(items, c)))
    ['foo', 1, 'bar', 2]
    

提交回复
热议问题