无限产量问题

发布于 2024-09-30 11:41:07 字数 636 浏览 5 评论 0原文

这是我的简单代码,

class Fibonacci:
    @staticmethod
    def series():
        fprev = 1
        fnext = 1
        yield fnext
        while True:
            yield fnext
            fprev,fnext = fnext,fprev+fnext

under10 = (i for i in Fibonacci.series() if i<10)
for i in under10 :
    print i

这绝对是显而易见的,但是......为什么解释器永远执行块

while True:
                yield fnext
                fprev,fnext = fnext,fprev+fnext

? 我在生成器中指定,我只想要元素<10

under10 = (i for i in Fibonacci.series() if i<10)

恕我直言,这是一个有点误解 有什么方法可以在不重写“系列”的情况下防止无限执行?

Here is my simple code

class Fibonacci:
    @staticmethod
    def series():
        fprev = 1
        fnext = 1
        yield fnext
        while True:
            yield fnext
            fprev,fnext = fnext,fprev+fnext

under10 = (i for i in Fibonacci.series() if i<10)
for i in under10 :
    print i

It's absolutely obvious, but...WHY interpreter is executing block

while True:
                yield fnext
                fprev,fnext = fnext,fprev+fnext

Forever?
I specified in generator,that I want only elements<10

under10 = (i for i in Fibonacci.series() if i<10)

IMHO, it's a little bit misunderstanding
Any way to prevent infinite execution without re-writing "series"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2024-10-07 11:41:07

解释器如何知道所有未来的数字将<< 10?它必须(以某种方式)知道它正在生成斐波那契数列,或者必须检查整个数列。

第一个它做不到,所以它做第二个。

您可以使用 itertools.takewhile

import itertools

under10 = itertools.takewhile(lambda n: n < 10, Fibonacci.series())

How should the interpreter know that all future numbers will be < 10? It would have to either know (somehow) that it’s churning out the Fibonacci series, or it would have to inspect the whole series.

It can’t do the first, so it does the second.

You can fix this by using itertools.takewhile:

import itertools

under10 = itertools.takewhile(lambda n: n < 10, Fibonacci.series())
ゝ偶尔ゞ 2024-10-07 11:41:07
under10 = (i for i in Fibonacci.series() if i<10) 

会继续下去,只是不会产生大于 10 的值。没有任何东西指示 for 循环停止。

你可能会更幸运地做这样的事情:

for i in Fibonacci.series():
    if i > 10:
        break
    #do your appends and such here

编辑:

我更喜欢 Konrad 的 itertools 示例,我总是忘记 itertools

under10 = (i for i in Fibonacci.series() if i<10) 

Will keep going, it just won't yield values greater than 10. There's nothing instructing the for loop to stop.

You would probably have better luck doing something like:

for i in Fibonacci.series():
    if i > 10:
        break
    #do your appends and such here

EDIT:

I like Konrad's itertools example much more, I always forget about itertools

演多会厌 2024-10-07 11:41:07

无限循环不是 Fibonacci.series() 方法中的 while True: 的结果。这是由 under10 = (i for i in Fibonacci.series() if i<10) 生成器引起的,该生成器一直持续运行,因为它没有意识到生成的值永远不会变小。这是同时修复它并概括它的[另一种]方法 - 无需重写 series() - 使用 itertools.takewhile() 迭代器:

import itertools
fibos_under = lambda N: itertools.takewhile(lambda f: f < N, Fibonacci.series())

for i in fibos_under(10):
    print i

顺便说一句: 您可以稍微简化 Fibonacci.series() 方法,将其更改为产生相同值的方法:

class Fibonacci:
    @staticmethod
    def series():
        fprev,fnext = 0,1
        while True:
            yield fnext
            fprev,fnext = fnext,fprev+fnext

The infinite loop isn't a result of the while True: in the Fibonacci.series() method. It's caused by the under10 = (i for i in Fibonacci.series() if i<10) generator which just keeps going since it doesn't realize the values yielded will never get smaller. Here's [another] way to fix it and generalize it at the same time -- without re-writing series() -- using the itertools.takewhile() iterator:

import itertools
fibos_under = lambda N: itertools.takewhile(lambda f: f < N, Fibonacci.series())

for i in fibos_under(10):
    print i

BTW: You can simplify the Fibonacci.series() method slightly by changing it to this which yields the same values:

class Fibonacci:
    @staticmethod
    def series():
        fprev,fnext = 0,1
        while True:
            yield fnext
            fprev,fnext = fnext,fprev+fnext
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文