Python:将列表转换为生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16041405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: convert list to generator
提问by swordholder
Say I have a list
说我有一个清单
data = []
data.append("A")
data.append("B")
data.append("C")
data.append("D")
How do I convert this to a generator? Any help with sample code would be highly appreciated...
我如何将其转换为生成器?对示例代码的任何帮助将不胜感激...
Found a URL: http://eli.thegreenplace.net/2012/04/05/implementing-a-generatoryield-in-a-python-c-extension/
找到一个网址:http: //eli.thegreenplace.net/2012/04/05/implementing-a-generatoryield-in-a-python-c-extension/
Is this what I want to do?
这是我想做的吗?
回答by Cameron Sparr
import itertools
iter_data = itertools.chain(data)
like so:
像这样:
In [10]: data
Out[10]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
In [11]: iter_data=itertools.chain(data)
In [12]: iter_data
Out[12]: <itertools.chain at 0x1ce8950>
In [13]: iter_data.next()
Out[13]: 1
回答by Yarkee
>>> d = ['A', 'B', 'C', 'D']
>>>
>>> def gen(d):
... for i in d:
... yield i
...
>>> for i in gen(d):
... print i
...
A
B
C
D
>>>
回答by Steven Rumbalski
The literal equivalent would be:
字面上的等价物是:
def data_generator():
yield 'A'
yield 'B'
yield 'C'
yield 'D'
Calling the generator function returns a generator-iterator. Passing the generator-iterator to the list constructor gives:
调用生成器函数返回一个生成器-迭代器。将生成器-迭代器传递给列表构造函数给出:
>>> list(data_generator())
['A', 'B', 'C', 'D']
This generator-iterator can also be created using a generator expression:
这个生成器-迭代器也可以使用生成器表达式来创建:
data_iter = (c for c in 'ABCD')
Note:You filled your list with four append statements. That's typically not how you would write it.
注意:您用四个 append 语句填充了您的列表。这通常不是您编写的方式。
Rather do:
而是这样做:
data = ['A', 'B', 'C', 'D']
回答by Ashwini Chaudhary
You could use a generator function:
您可以使用生成器函数:
def gen():
for x in "ABCD":
yield x
In [9]: it = gen()
In [10]: next(it)
Out[10]: 'A'
In [11]: next(it)
Out[11]: 'B'
In [12]: next(it)
Out[12]: 'C'
In [13]: next(it)
Out[13]: 'D'
回答by David
>>> (n for n in [1, 2, 3, 5])
<generator object <genexpr> at 0x02A52940>
works in Python 2.7.4+
适用于 Python 2.7.4+
>>> a2g = lambda x : (n for n in x)
>>> a2g([1, 2, 3, 4, 5])
<generator object <genexpr> at 0x02A57CD8>
Edit:
编辑:
One more slight variation of a lambda generator factory pattern
lambda 生成器工厂模式的另一种细微变化
>>> a2g = lambda *args: (n for n in args)
>>> for i in a2g(1, 2, 3, 4, 5): print i
1
2
3
4
5
回答by Micaele
(item for item in listNameHere).next()
This is the general way of doing it, if I recall correctly.
如果我没记错的话,这是这样做的一般方法。
>>> a = [0,1,2,3,4]
>>> x = (item for item in a)
>>> x
<generator object <genexpr> at 0x028B8440>
>>> x.next()
0
>>> x.next()
1
>>> x.next()
2
etc etc.
等等等等
回答by Heinrich Hartmann
Are you sure, you want to create a generator? A generator is function which returns an iterator type, constructed e.g. by using the yieldkeyword (cf. term-generator).
If you really want this, steven-rumbalski's answer is precisely what you are looking for:
您确定要创建生成器吗?生成器是返回迭代器类型的函数,例如通过使用yield关键字(参见term-generator)构造。如果你真的想要这个,steven-rumbalski的答案正是你正在寻找的:
data_gen = (y for y in data)
Most of the time, you will want to directly create an iteratorobject, e.g. to use the next()method. In this case, the answer is implicit in the comment by mgilsonabove:
大多数时候,您会想要直接创建一个迭代器对象,例如使用该next()方法。在这种情况下,答案隐含在上面mgilson的评论中:
data_iter = iter(data)
which is equivalent to data_iter = data.__iter__(), cf. functions#iter.
这相当于data_iter = data.__iter__(),参见。功能#iter。

