为什么Python不允许在同一行上放置for后跟if?

时间:2022-04-10 16:44:56

Why is this code

为什么这个代码

for i in range(10):
    if i == 5: print i

valid while the compound statement (I know that PEP 8 discourages such coding style)

复合语句时有效(我知道PEP 8不鼓励这样的编码风格)

for i in range(10): if i == 5: print i

is not?

不是?

3 个解决方案

#1


7  

This is because python has strict rules about indentation being used to represent blocks of code and by putting an for followed by an if, you create ambiguous indentation interpretations and thus python does not allow it.

这是因为python有关于缩进的严格规则,用于表示代码块,并且通过放置for后跟if,您创建不明确的缩进解释,因此python不允许它。

For python, you can put as many lines as you want after a if statement:

对于python,您可以在if语句后添加任意数量的行:

if 1==1: print 'Y'; print 'E'; print 'S'; print '!';

as long as they all have the same indentation level, i.e., no if, while, for as they introduce a deeper indentation level.

只要它们都具有相同的缩进级别,即,如果,因为它们引入了更深的缩进级别。

Hope that helps

希望有所帮助

#2


6  

The reason why you cannot is because the language simply doesn't support it:

你不能这样做的原因是语言根本不支持它:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

It has been suggested many times on the Python mailing lists, but has never really gained traction because it's already possible to do using existing mechanisms...

在Python邮件列表上已多次提出建议,但从未真正获得牵引力,因为已经可以使用现有机制...

Such as a filtered generator expression:

如过滤后的生成器表达式:

for i in (i for i in range(10) if i == 5):
    ...

The advantage of this over the list comprehension is that it doesn't generate the entire list before iterating over it.

这比列表理解的优点是它在迭代之前不会生成整个列表。

#3


1  

using list comprehension:

使用列表理解:

In [10]: [x for x in range(10) if x ==5][0]
Out[10]: 5

#1


7  

This is because python has strict rules about indentation being used to represent blocks of code and by putting an for followed by an if, you create ambiguous indentation interpretations and thus python does not allow it.

这是因为python有关于缩进的严格规则,用于表示代码块,并且通过放置for后跟if,您创建不明确的缩进解释,因此python不允许它。

For python, you can put as many lines as you want after a if statement:

对于python,您可以在if语句后添加任意数量的行:

if 1==1: print 'Y'; print 'E'; print 'S'; print '!';

as long as they all have the same indentation level, i.e., no if, while, for as they introduce a deeper indentation level.

只要它们都具有相同的缩进级别,即,如果,因为它们引入了更深的缩进级别。

Hope that helps

希望有所帮助

#2


6  

The reason why you cannot is because the language simply doesn't support it:

你不能这样做的原因是语言根本不支持它:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

It has been suggested many times on the Python mailing lists, but has never really gained traction because it's already possible to do using existing mechanisms...

在Python邮件列表上已多次提出建议,但从未真正获得牵引力,因为已经可以使用现有机制...

Such as a filtered generator expression:

如过滤后的生成器表达式:

for i in (i for i in range(10) if i == 5):
    ...

The advantage of this over the list comprehension is that it doesn't generate the entire list before iterating over it.

这比列表理解的优点是它在迭代之前不会生成整个列表。

#3


1  

using list comprehension:

使用列表理解:

In [10]: [x for x in range(10) if x ==5][0]
Out[10]: 5