"对于"与“其他”? [英] "for" with "else"?

查看:66
本文介绍了"对于"与“其他”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试打印无空列表时,我意外地将else

语句添加为for。而不是如果。这就是我所拥有的:


if(len(mylist)> 0):

for x,y in mylist:

打印x,y

其他:

打印空列表


原本应该是:


if(len(mylist)> 0):

for x,y in mylist:

print x,y

else:

打印空列表

这是预期的吗?

(python 2.2.2)


+++++++++++++++++++++++++++++++++++++++++++++++ >

for x in range(5):



.... print x * x

....其他:

....打印完成

....

0

1

4

9



已完成

+++++++++++++++++++++++++++++++++++++ =h2_lin>解决方案

[无效用户写道]

尝试时打印一个无空列表,我不小心把一个else
语句带有for和for。而不是如果。这就是我所拥有的:




来自Python语言参考


"""

for语句


for_stmt :: =" for" target_list" in" expression_list":"套房

[" else" ":"套件]


表达式列表评估一次;它应该产生一个序列。然后对序列中的每个项目执行一次

套件,按升序索引的

顺序执行。依次使用标准的分配规则将每个项目分配到

目标列表,然后执行

套件。当项目耗尽时(当序列为空时立即

),else子句中的套件(如果存在)执行

,循环终止。

"""

http://www.python.org/doc/current/ref/for.html

快速浏览档案确认至少从python 1.4开始这是

的情况: -

http://www.python.org/doc/1.4/ref/ref7.html#HDR2


问候,


-

alan kennedy

------------ -----------------------------------------

check http标题: http://xhaus.com/headers

email alan: http://xhaus.com/mailto/alan


无效用户< us ** @无效。域>写道:

这是预期的吗?
(python 2.2.2)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++ + + for x in range(5):.... print x * x
.... else:
.... print" done"
.... 0
1
4
完成
+++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++如果for循环正常终止,则调用else子句。

对比:


++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ b ....如果x == 4:

....休息

....其他:

....打印全部完成

....

0

1

4

9

16



++++++++++++++++++++++++++++++++ +++++++++++++++++++++++似乎更自然,如果语法意味着做这个循环到

完成,否则做这个额外的东西,但这是它的方式和

必须只记得它。)


-

Mark Jackson - http://www.alumni.caltech.edu/~mjackson

没有在他不再提问之前,没有人会变成傻瓜。

- Charles P. Steinmetz


无效用户写道:

在尝试打印无空列表时,我不小心将else
语句添加为for。而不是如果。这就是我所拥有的:

if(len(mylist)> 0):
for x,y in mylist:
print x,y
else:
打印空列表

应该是:

if(len(mylist)> 0):
for x, y in mylist:
打印x,y
否则:
打印空列表

这是预期的吗?




当然。执行else分支,除非for提前终止

(通过break,return或exception) - 就像while循环的

else分支一样。


典型用例:

$ 4 $ b for allitems中的商品:

if fraboozable(item):

打印第一个fraboozable项目是,项目

休息

其他:

打印对不起,没有项目是fraboozable

Alex


While trying to print a none empty list, I accidentaly put an "else"
statement with a "for" instead of "if". Here is what I had:

if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:
print "Empty list"

which was supposed to be:

if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:
print "Empty list"
Is this to be expected?
(python 2.2.2)

++++++++++++++++++++++++++++++=

for x in range(5):


.... print x*x
.... else:
.... print "done"
....
0
1
4
9
16
done
++++++++++++++++++++++++++++++=

解决方案

[Invalid User wrote]

While trying to print a none empty list, I accidentaly put an "else"
statement with a "for" instead of "if". Here is what I had:



From the Python Language Reference

"""
The for statement

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

The expression list is evaluated once; it should yield a sequence. The
suite is then executed once for each item in the sequence, in the
order of ascending indices. Each item in turn is assigned to the
target list using the standard rules for assignments, and then the
suite is executed. When the items are exhausted (which is immediately
when the sequence is empty), the suite in the else clause, if present,
is executed, and the loop terminates.
"""

http://www.python.org/doc/current/ref/for.html

A quick glance through the archives confirms that this has been the
case since at least python 1.4:-

http://www.python.org/doc/1.4/ref/ref7.html#HDR2

regards,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan


Invalid User <us**@invalid.domain> writes:

Is this to be expected?
(python 2.2.2)

++++++++++++++++++++++++++++++=

>>> for x in range(5): .... print x*x
.... else:
.... print "done"
....
0
1
4
9
16
done
++++++++++++++++++++++++++++++=



Yes. The else clause is invoked iff the for loop terminates normally.
Contrast with:

++++++++++++++++++++++++++++++

for x in range(5): .... print x*x
.... if x==4:
.... break
.... else:
.... print "did all"
....
0
1
4
9
16


++++++++++++++++++++++++++++++

(This happens to be the *reverse* of what my intuition expects - it
would seem more natural if the syntax meant "do this for loop to
completion, else do this extra stuff," but this is the way it is and
one must simply remember it.)

--
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
There are no foolish questions and no man becomes a fool
until he has stopped asking questions.
- Charles P. Steinmetz


Invalid User wrote:

While trying to print a none empty list, I accidentaly put an "else"
statement with a "for" instead of "if". Here is what I had:

if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:
print "Empty list"

which was supposed to be:

if ( len(mylist)> 0) :
for x,y in mylist:
print x,y
else:
print "Empty list"
Is this to be expected?



Sure. The else branch is executed unless the for terminates
prematurely (via break, return or exception) -- just like the
else branch of a while loop.

Typical use case:

for item in allitems:
if fraboozable(item):
print "first fraboozable item is", item
break
else:
print "Sorry, no item is fraboozable"
Alex


这篇关于&QUOT;对于&QUOT;与“其他”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆