python的yield是否意味着继续?

发布于 2024-09-10 08:50:48 字数 422 浏览 6 评论 0原文

我有一个 for 循环来检查一系列条件。在每次迭代中,它应该仅针对其中一个条件产生输出。最终产量是默认值,以防所有条件都不成立。我是否必须在每个收益块后面添加一个继续

def function():
    for ii in aa:
       if condition1(ii):
           yield something1
           yield something2
           yield something3
           continue

       if condition2(ii):
           yield something4
           continue

       #default
       yield something5
       continue

I have a for loop that checks a series of conditions. On each iteration, it should yield output for only one of the conditions. The final yield is a default, in case none of the conditions are true. Do I have to put a continue after each block of yields?

def function():
    for ii in aa:
       if condition1(ii):
           yield something1
           yield something2
           yield something3
           continue

       if condition2(ii):
           yield something4
           continue

       #default
       yield something5
       continue

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

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

发布评论

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

评论(6

调妓 2024-09-17 08:50:48

不,yield 并不意味着继续,它只是从下一行开始,下一次。一个简单的例子演示了

def f():
    for i in range(3):
        yield i
        print i,

list(f())

This 打印 0,1,2 但如果yield 继续,它不会

NO, yield doesn't imply continue, it just starts at next line, next time. A simple example demonstrates that

def f():
    for i in range(3):
        yield i
        print i,

list(f())

This prints 0,1,2 but if yield continues, it won't

自找没趣 2024-09-17 08:50:48

我建议使用 elif 和 else 语句,而不是使用 continue 语句:

def function():
    for ii in aa:
       if condition1(ii):
           yield something1
           yield something2
           yield something3

       elif condition2(ii):
           yield something4

       else: #default
           yield something5

这对我来说似乎更具可读性

Instead of using the continue statement I would suggest using the elif and else statments:

def function():
    for ii in aa:
       if condition1(ii):
           yield something1
           yield something2
           yield something3

       elif condition2(ii):
           yield something4

       else: #default
           yield something5

This seems much more readable to me

飞烟轻若梦 2024-09-17 08:50:48

Python 中的 yield 停止执行并返回值。当再次调用迭代器时,它会在 yield 语句之后直接继续执行。例如,定义为的生成器

def function():
    yield 1
    yield 2

将依次返回 1 然后 2。换句话说,需要继续。然而,在这种情况下,flashk 所描述的 elifelse 绝对是正确的工具。

yield in Python stops execution and returns the value. When the iterator is invoked again it continues execution directly after the yield statement. For instance, a generator defined as:

def function():
    yield 1
    yield 2

would return 1 then 2 sequentially. In other words, the continue is required. However, in this instance, elif and else as flashk described are definitely the right tools.

咋地 2024-09-17 08:50:48

continue 会跳过剩余的代码块,但是当在生成器上再次调用 next() 时,会执行 yield 之后的代码块。 yield 的作用就像在某个点暂停执行。

continue skips the remaining code block, but the code block after yield is executed when next() is called again on the generator. yield acts like pausing execution at certain point.

爱的那么颓废 2024-09-17 08:50:48

这个方式比较清晰了,希望有帮助,也感谢Anurag Uniyal。

def f():
    for i in range(3):
        yield i
        print(i+10)

list(f())

----------- 运行后 ------------

10
11
12
[0, 1, 2]

This way is more clear, hope it helps, also thanks Anurag Uniyal.

def f():
    for i in range(3):
        yield i
        print(i+10)

list(f())

----------- after run ------------

10
11
12
[0, 1, 2]
↙温凉少女 2024-09-17 08:50:48

如果某事是简单值并且条件是检查相等性,我更愿意进行此“案例结构”字典查找:

ii_dict={'a':('somethinga1','somethinga2','somethinga3'),'b':('somethingb1',)}
ii_default = ('somethingdefault',)
aa='abeabbacd'

def function():
    return (value
           for ii in aa
           for value in (ii_dict[ii] if ii in ii_dict else ii_default))

for something in function(): print something

If the something is simple value and conditions are checks for equality, I would prefer to make this "case structure" dictionary lookup:

ii_dict={'a':('somethinga1','somethinga2','somethinga3'),'b':('somethingb1',)}
ii_default = ('somethingdefault',)
aa='abeabbacd'

def function():
    return (value
           for ii in aa
           for value in (ii_dict[ii] if ii in ii_dict else ii_default))

for something in function(): print something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文