How do I merge two python iterators?

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

    I'm not sure what your application is, but you might find the enumerate() function more useful.

    >>> items = ['foo', 'bar', 'baz']
    >>> for i, item in enumerate(items):
    ...  print item
    ...  print i
    ... 
    foo
    0
    bar
    1
    baz
    2
    

提交回复
热议问题