简体   繁体   English

在Python中使用yield?

[英]Using yield in Python?

I have this code: 我有以下代码:

def generator(n):
    list_of = range(1,n+1)
    for i in list_of:
        if i % 7 == 0:
            yield i

print generator(100)

This should print all the numbers in the given range that are divisible by 7 , but the output is <generator object generator at 0x1004ad280> instead. 这将打印给定范围内所有可被7整除的数字,但输出改为<generator object generator at 0x1004ad280>

Also, the word yield in my text editor (KOD) doesn't appear highlighted in sky blue color like all the reserved words but instead appears in white, is that fine? 此外,我的文本编辑器(KOD)中的单词yield不会像所有保留单词一样以天蓝色突出显示,而是以白色显示,这样还好吗?

Your generator works . 您的发电机可以工作 You forgot to iterate over it though: 但是,您忘记了对其进行迭代:

for elem in generator(100):
    print elem

or you could turn it into a list: 或者您可以将其变成一个列表:

print list(generator(100))

You instead printed the generator object produced by calling the generator function. 而是打印通过调用generator函数产生的generator对象。 A generator function produces a suspended generator. 发电机功能产生悬挂的发电机。 Only when you iterate over it is code executed (until the next yield ). 仅当您对其进行迭代时,才执行代码(直到下一个yield )。

Demo: 演示:

>>> def generator(n):
...     list_of = range(1,n+1)
...     for i in list_of:
...         if i % 7 == 0:
...             yield i
... 
>>> print list(generator(100))
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]

The list() call iterates over the given argument, producing a Python list object with all elements yielded by the argument. list()调用在给定参数上进行迭代,从而生成一个Python列表对象,其中包含由该参数产生的所有元素。 Great for iterating over a generator to pull in all the elements that it produces. 非常适合迭代生成器以提取生成的所有元素。

As for KOD; 至于KOD; that editor hasn't seen updates in years now; 那位编辑器已经好几年没有看到更新了; you may want to switch to something else. 您可能需要切换到其他位置。 As the KOD twitter feed stated 2 years ago : 正如KOD Twitter提要2年前所说

Don't wait for me, I'm kind of like a Zombie. 不要等我,我有点像僵尸。 Go get Sublime Text @sublimehq which is awesome: http://www.sublimetext.com 去获得很棒的Sublime Text @sublimehqhttp : //www.sublimetext.com

I concur; 我同意; Sublime Text is my current editor of choice. Sublime Text是我当前选择的编辑器。

Generators functions allow you to declare a function that behaves like an iterator, ie it can be used in a for loop. 生成器函数使您可以声明一个行为类似于迭代器的函数,即可以在for循环中使用它。 u can learn Here: generator 你可以在这里学习: 发电机

def generator(n):
        list_of = range(1,n+1)
        for i in list_of:
            if i % 7 == 0:
                yield i

    for i in generator(100):
        print i

or 要么

You can use next(generator(100)) to print one element at top 您可以使用next(generator(100))在顶部打印一个元素

or 要么

list(generator(100))

When calling the generator function you receive generator object. 调用生成器函数时,您会收到生成器对象。 In order to get values you should iterate over this object. 为了获得值,您应该遍历此对象。 In you case you can do list(generator(100)) 如果您可以list(generator(100))

But this doesn't make sense. 但这没有道理。 Use list comprehension if you need list: 如果需要列表,请使用列表理解:

[x for x in range(1, 101) if x % 7 == 0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM