If a python iterator returns iterable objects, how can I chain those objects into one big iterator?

廉价感情. 提交于 2019-12-06 04:24:23

问题


I'll give a simplified example here. Suppose I have an iterator in python, and each object that this iterator returns is itself iterable. I want to take all the objects returned by this iterator and chain them together into one long iterator. Is there a standard utility to make this possible?

Here is a contrived example.

x = iter([ xrange(0,5), xrange(5,10)])

x is an iterator that returns iterators, and I want to chain all the iterators returned by x into one big iterator. The result of such an operation in this example should be equivalent to xrange(0,10), and it should be lazily evaluated.


回答1:


You can use itertools.chain.from_iterable

>>> import itertools
>>> x = iter([ xrange(0,5), xrange(5,10)])
>>> a = itertools.chain.from_iterable(x)
>>> list(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

If that's not available on your version (apparently, it's new in 2.6), you can just do it manually:

>>> x = iter([ xrange(0,5), xrange(5,10)])
>>> a = (i for subiter in x for i in subiter)
>>> list(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



回答2:


Here's the old-fashioned way:

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import generators
>>> def mychain(iterables):
...     for it in iterables:
...         for item in it:
...             yield item
...
>>> x = iter([ xrange(0,5), xrange(5,10)])
>>> a = mychain(x)
>>> list(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

I'm not sure what the purpose or benefit of using iter() is, in this case:

>>> x = [xrange(0,5), xrange(5,10)]
>>> a = mychain(x)
>>> list(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>


来源:https://stackoverflow.com/questions/4302201/if-a-python-iterator-returns-iterable-objects-how-can-i-chain-those-objects-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!