Python 2.4 | 7.3 for语句 [英] Python 2.4 | 7.3 The for statement

查看:67
本文介绍了Python 2.4 | 7.3 for语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的名字是Juan Carlos Rodrigo,我喜欢Python。

这是我见过的最令人印象深刻且实用的语言




我正在学习,在 http://www.uoc.edu ,一个信息

技术研究生。我在我过去的工作中编写了一些

REXX应用程序(所以我喜欢python,

不再结束)。


**从Python-Dev中重新发布一些评论。


-------- 8< -------- 8< ------ --8< -------- 8< -------- 8< -------- 8< --------


Python 2.4 | 7.3 for声明:

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


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

suite [" else" ":"套房]

新陈述:

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


for_stmt :: =" for target_list" in" expression_list

["和表达式"

suite [" else ":"套件]


**如果表达式在

进入for之前评估为False,则跳转到其他位置。

**如果

第一次迭代后,表达式被评估为False,休息。

那么?我们可以用这个新的做什么?,

和?它会避免线路超过?:


我的第二个评论是我们的智力相当于

适合掌握静态关系和我们的权力

可视化流程的演变过程相对较差

开发。 [1]

如果我们事先看到它会更容易:

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


假=假

alist = [1,2,3,3,4 ,5,6,7,8,9]

for alist中的项目而不是离开:

如果item为1:leave = True

避免代码超出:

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


a = 1

b = 2

c = 3

alist = [1,2,3,4,5,6,7,8,9]
for alist中的项目和< 2和b< 3和c< 4:

if item == 3:a + = 1

if item == 2:b + = 1

if item == 1:c + = 1

print"%d%d%d" %(a,b,c)

#三行关闭(休息时减少25%)

其他功能和其他功能:

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


alist = [1,2,3]

enter = False

如果list [0] == 4:

enter = True

for alist and item输入:

打印商品

else:

打印没有帐号

真正的问题:

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


"翻译任意流程图的练习或

机械地减少跳跃,但不建议使用
。 [1]


好​​的,不推荐大家,但Python应该可以,

然后人们会选择。

[1]转到认为有害的陈述

Edsger W. Dijkstra
http://www.acm.org/classics/oct95/

PD:你的工作令人印象深刻,谢谢。


-------- 8< -------- 8< -------- 8< ------- -8< -------- 8< -------- 8< --------


+++ Andrew Koenig写道:

for_stmt :: =" for target_list" in" expression_list
["和表达式:"
套件[" else" ":"套件]
它不能工作。 expression_list可能只是一个变量,因为表达式可以是
,在这种情况下,你得到
for target_list" in"变量和变量:


考虑到您的定义不正确。

,当然还有
变量"和变量
已经是一个表达式。所以这是模棱两可的。


没有我的定义没有歧义,你的想法确实没有像你想象的那样工作
因为不是你的表达

错误地写出来,是一个expression_list:

leave = True
l = [1 ,2,3,4]
表格中的项目并留下:
文件"< stdin>",第1行

表单中的项目并离开:


你的例子真正做的是迭代

表达式列表(BTW:和不能用,因为你指出):

leave = True
l = [1,2,3,4]
对于l中的项目,请离开:



....打印商品

....

[1,2,3,4]

真实
+++ Nick Coghlan写道:

有趣的想法,但鉴于
存在break语句,并不是真的需要:
for alist中的项目:
如果item为1:
打破


嗯尼克这里的观点是有可能

离开for循环而不使用break语句,

和当然也使用休息。

+++ Nick Coghlan写道:

所有非顺序控制结构仅仅是使用goto(底层机器)的约束方式代码将转换为条件
和无条件分支和跳转 - 即goto''。


毫无疑问,尼克,但他们的编程非常非常高,而且是b $ b语言。


____________________________________________ ''break''是一种高度约束的goto形式和结构化编程的基本部分(就像''继续'')


你表达得更好比我好。

我曾经分享你对休息和继续的看法 -
经验(尤其是Python体验)已经说服了我。
Python接受了突破循环的概念
它甚至在循环结构上有一个''else''子句,只有当循环自然退出时才执行
而不是通过休息
的声明。


并且通过打破可能性会更好。

无论你是否亲自选择使用休息并且
继续,这些陈述的存在是主要的原因
为for循环添加一个标志条件是不可能的。




?为什么C既有破,继续又有破坏

的能力?


感谢您的评论。
< br>

解决方案

brainsucker写道:

Python 2.4 | 7.3 for声明:
-----------------------------------

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

新陈述:
------------------

for_stmt :: ="为" target_list" in" expression_list
["和表达式:"
套件[" else" ":"套件]

**如果在进入for之前表达式的计算结果为False,则跳转到其他位置。
**如果表达式在第一次被评估为False之后迭代,休息。




我认为你的想法很好,但正如其他人所说的那样和文字

可能令人困惑。 ?也许我们可以使用另一个单词而不是和?


for定义可以是这样的:


for_stmt :: = "对于" target_list" in" expression_list

[" until表达式"

suite [" else ":"套件]


或其他一些澄清表达工作的词

leave_cond_1 = False

leave_cond_2 =错误

mylist = [1,2,3,4,5]

for mylist中的项目,直到leave_cond_1或leave_cond_2:

打印项目


2005年3月22日06:32:38 -0800,他******** @ gmail.com

< he ************************* @ gmail.com>写道:

for定义可能是这样的:

for_stmt :: =" for target_list" in" expression_list
[" until表达式:"
套件[" else" ":"套件]

或其他一些澄清表达工作的词

leave_cond_1 = False
leave_cond_2 = False
mylist = [1,2, 3,4,5]
用于mylist中的项目,直到leave_cond_1或leave_cond_2:
打印项目




不过,这可以通过休息来完成使用更少的变量(这意味着更少的工作用于gc和

每个人)。


在你的例子中,不是验证某些条件并设置

leave_cond_n为True,你只需检查相同的条件,然后使用

break。


.. Facundo


博客: http://www.taniquetil.com.ar/plog/

PyAr: http://pyar.decode.com.ar/


>不过,这可以用日e break语句,用更多

清晰的方式,用更少的变量(这意味着gc和
每个人的工作量减少)。




很高兴给你读弗朗西斯科。 :)继续努力工作,谢谢。


我一直在和那些Python程序员(和角色扮演者)谈话,

:)我已经做了一些代码,你可以避免使用和关键字:

cordure = 54
monster = [" Tchulu"," Golum", Cerberus对于哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇0:
如果wow ==" Tchulu":cordure - = 30
elif wow ==" Cerberus":cordure - = 20
elif wow ==" Golum":cordure - = 5
如果确定< = 0:
打印结束游戏
否则:
打印下一个任务



这是你的版本:

cordure = 54
monster = [" Tchulu"," Golum"," ; Cerberus哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇> elif wow ==" Golum":cordure - = 5
if cordure< 0:休息#:|
如果有线< = 0:
打印结束游戏
否则:
打印下一个任务



a)正如你所看到的,还有一行代码...

另外,在你的代码中我事先没有看到

当这个角色扮演者没有使用时。

#小事情很重要。 :)

b)鉴于你的IF条件被评估

每次迭代,我看到代码中的一行少,

评估相同的条件,每个

迭代。

c)我不想这样说:|


+调试和朋友:

+工具>搜索

+休息


ummmm在新的中我可以看到发生了什么

事先发生,所以我可以搜索更快

调试时:


" ...我们的智力相当于

适合掌握静态关系...... " [1]

d)我只是不会更加强大,只需要令人印象深刻的g />
Python for循环,我们可以拥有它。

[1]转到声明被认为是有害的

Edsger W. Dijkstra
http://www.acm.org/classics/oct95/

PD1:弗朗西斯科我们​​在IRC上讨论了这个问题,

所提供的代码是以某种方式表达的代码来表达

我(有人?)希望Python做什么。


PD2:试着想象嵌套循环与那些

200+行(可能是坏编码:?,或者不是!)

并尝试调试所有这些BREAKS。


Hi My name is Juan Carlos Rodrigo, and I love Python.
It is the most impressive and usefull language that
I have ever seen.

I am studing, at http://www.uoc.edu, an Information
Technology Postgraduate. And I have programmed some
REXX applications in my past Jobs (So I love python,
no more ENDS).

** Reposting from Python-Dev with some more comments.

--------8<--------8<--------8<--------8<--------8<--------8<--------

Python 2.4 | 7.3 The for statement:
-----------------------------------

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

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

** If the expression evaluates to False before
entering the for, jump else.
** If the expression is evaluated to False after
the first iteration, break.
So ?What we can do with this new for?,
and ?It is going to avoid line exceed?:

"My second remark is that our intellectual powers are rather
geared to master static relations and that our powers to
visualize processes evolving in time are relatively poorly
developed." [1]
It is easier if we see it beforehand:
-------------------------------------

leave = False
alist = [1,2,3,3,4,5,6,7,8,9]
for item in alist and not leave:
if item is 1: leave = True
Avoiding code exceed:
---------------------

a = 1
b = 2
c = 3
alist = [1,2,3,4,5,6,7,8,9]
for item in alist and a < 2 and b < 3 and c < 4:
if item == 3: a += 1
if item == 2: b += 1
if item == 1: c += 1
print "%d %d %d" % (a,b,c)
# three lines off (25% less on breaks)
Other features and the else:
----------------------------

alist = [1,2,3]
enter = False
if list[0] == 4:
enter = True
for item in alist and enter:
print item
else:
print "No account"
The real problem:
-----------------

"The exercise to translate an arbitrary flow diagram more or
less mechanically into a jump-less one, however, is not to be
recommended." [1]

Ok, it''s not recommended, at large, but Python should make it possible,
and then the people will choose.
[1] Go To Statement Considered Harmful
Edsger W. Dijkstra
http://www.acm.org/classics/oct95/

PD: Your work is impressive, thanks.

--------8<--------8<--------8<--------8<--------8<--------8<--------

+++ Andrew Koenig wrote:

for_stmt ::= "for" target_list "in" expression_list
[ "and" expression ] ":"
suite ["else" ":" suite] It can''t work. The expression_list could be just a variable, as could the expression, in which case you get "for" target_list "in" variable "and" variable ":"
Considering that your for definition is not correct.
and, of course variable "and" variable is already an expression. So it''s ambiguous.
No my definition has no ambiguity in it and your idea does
not work as you think, because is not an expression as you
incorrectly wrote out, is an expression_list:

leave = True
l = [1,2,3,4]
form item in l and leave: File "<stdin>", line 1
form item in l and leave:

And your example what really does is iterating over the
expression_list (BTW: "and" cannot be used as you point out):
leave = True
l = [1,2,3,4]
for item in l, leave:


.... print item
....
[1, 2, 3, 4]
True
+++ Nick Coghlan wrote:
Interesting idea, but not really needed given the
existence of the break statement: for item in alist:
if item is 1:
break
Well Nick the point here is to have the possibility
to leave for loops without using the break statement,
and of course use breaks too.
+++ Nick Coghlan wrote:
All non-sequential control structures are merely constrained ways of
using goto (the underlying machine code will devolve into conditional and unconditional branches and jumps - i.e. goto''s).
No doubt Nick, but were are programming on a very, very high level
language.

____________________________________________ ''break'' is a highly constrained form of goto and a fundamental part
of structured programming (as is ''continue'')
Well you are expressing it better than I am.
I used to share your sentiment regarding break and continue -
experience (especially Python experience) has convinced me otherwise. Python embraces the concept of breaking out of a loop to the point
that it even has an ''else'' clause on loop structures that is executed only if the loop is exited naturally rather than via a break statement.

And It will be better with by for breaking possibilities.
Regardless of whether you personally choose to use break and
continue, the existence of those statements is the main reason
that the addition of a flag condition to for loops is highly
unlikely.



?Why is that C has both break, continue and in for breaking
capabilities?

Thanks for your comments.

解决方案

brainsucker wrote:

Python 2.4 | 7.3 The for statement:
-----------------------------------

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

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

** If the expression evaluates to False before
entering the for, jump else.
** If the expression is evaluated to False after
the first iteration, break.




I think that your idea is good but as others said the "and" literal
could be confusing. ?Maybe we can use another word instead of "and"?

The for definition could be like this:

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

or some other word that clarifies the work of the expression

leave_cond_1 = False
leave_cond_2 = False
mylist = [1,2,3,4,5]
for item in mylist until leave_cond_1 or leave_cond_2:
print item


On 22 Mar 2005 06:32:38 -0800, he*************************@gmail.com
<he*************************@gmail.com> wrote:

The for definition could be like this:

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

or some other word that clarifies the work of the expression

leave_cond_1 = False
leave_cond_2 = False
mylist = [1,2,3,4,5]
for item in mylist until leave_cond_1 or leave_cond_2:
print item



Still, this can be acomplished with the break statement, in a more
clear way, with less variables (which implies less work for the gc and
everybody).

In your example, instead of verifying some condition and setting
leave_cond_n to True, you just check the same condition, and use
break.

.. Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://pyar.decode.com.ar/


>Still, this can be acomplished with the break statement, in a more

clear way, with less variables (which implies less work for the gc and
everybody).



Glad to read from you Francisco. :) Keep up that hard work, thanks.

I have been talking with those Python programmers (And Role players),
:) and I have made a bit of code for you avoiding the "and" keyword:

cordure = 54
monster = [ "Tchulu", "Golum", "Cerberus"]
for wow in monster until cordure < 0:
if wow == "Tchulu": cordure -= 30
elif wow == "Cerberus": cordure -= 20
elif wow == "Golum": cordure -= 5
if cordure <= 0:
print "End game"
else:
print "Next task"

This is your version:
cordure = 54
monster = [ "Tchulu", "Golum", "Cerberus"]
for wow in monster:
if wow == "Tchulu": cordure -= 30
elif wow == "Cerberus": cordure -= 20
elif wow == "Golum": cordure -= 5
if cordure < 0: break # :|
if cordure <= 0:
print "End game"
else:
print "Next task"


a) As you can see, one more line of code...
Plus, in your code I do not see beforehand
when this roleplayer runs out of cordure.
# Little things matter. :)
b) Given that your IF condition is evaluated
ON every iteration, I see one line less on code,
evaluating the SAME conditions, for every
iteration.
c) I did not want to say this :|

+Debugging and friends:
+Tools > Search
+Break

ummmm in the new for I can see what is going
to happend beforehand, so I can search faster
when debugging:

"...our intellectual powers are rather
geared to master static relations..." [1]
d) I just wont more power on the just impressive
Python for loop, we can have it all.
[1] Go To Statement Considered Harmful
Edsger W. Dijkstra
http://www.acm.org/classics/oct95/

PD1: Francisco we have talked about this on the IRC,
the code presented is somehow TOY code to express
what I (Somebody?) want Python to do.

PD2: Try to imagine that nested loop with those
200+ lines in it ( Probably is bad coded :?, or not! )
and try to debug all those BREAKS.


这篇关于Python 2.4 | 7.3 for语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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