我需要在 Python 程序中模拟一个 do-while 循环。不幸的是,以下简单的代码不起作用:
list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None
while True:
if element:
print element
try:
element = iterator.next()
except StopIteration:
break
print "done"
它打印以下输出,而不是“1,2,3,done”:
[stdout:]1
[stdout:]2
[stdout:]3
None['Traceback (most recent call last):
', ' File "test_python.py", line 8, in <module>
s = i.next()
', 'StopIteration
']
我该怎么做才能捕获“停止迭代”异常并正确中断 while 循环?
下面以伪代码的形式显示了为什么需要这样的东西的示例。
状态机:
s = ""
while True :
if state is STATE_CODE :
if "//" in s :
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = STATE_COMMENT
else :
tokens.add( TOKEN_CODE, s )
if state is STATE_COMMENT :
if "//" in s :
tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
else
state = STATE_CODE
# Re-evaluate same line
continue
try :
s = i.next()
except StopIteration :
break
s=i.next()
而不是 None 并且可能会做一些初始工作,而不是让您的第一次通过循环无用。
我不确定您要做什么。你可以像这样实现一个 do-while 循环:
while True:
stuff()
if fail_condition:
break
或者:
stuff()
while not fail_condition:
stuff()
你在做什么尝试使用 do while 循环来打印列表中的内容?为什么不直接使用:
for i in l:
print i
print "done"
更新:
那么你有一个行列表吗?你想继续迭代它吗?怎么样:
for s in l:
while True:
stuff()
# use a "break" instead of s = i.next()
这看起来像你想要的吗?使用您的代码示例,它将是:
for s in some_list:
while True:
if state is STATE_CODE:
if "//" in s:
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = STATE_COMMENT
else :
tokens.add( TOKEN_CODE, s )
if state is STATE_COMMENT:
if "//" in s:
tokens.append( TOKEN_COMMENT, s.split( "//" )[1] )
break # get next s
else:
state = STATE_CODE
# re-evaluate same line
# continues automatically
这是模拟 do-while 循环的一种非常简单的方法:
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
do-while 循环的关键特性是循环体总是至少执行一次,并且条件在循环体的底部进行评估。此处显示的控制结构无需异常或中断语句即可完成这两项工作。它确实引入了一个额外的布尔变量。
break
灵活。具体来说,如果在 test_loop_condition()
之后需要逻辑,一旦我们完成就不应执行,它必须包装在 if condition:
中。顺便说一句,condition
含糊不清。更具描述性:more
或 notDone
。
break
,当我在我维护的代码中遇到它时,我发现循环通常可以不用它来编写。所提出的解决方案是,IMO,在 python 中表示 do while 构造的 最清晰 方式。
has_no_errors
或 end_reached
(在这种情况下,循环将开始 while not end_reached
我下面的代码可能是一个有用的实现,突出了我理解的 do-while 与 while 之间的主要区别。
因此,在这种情况下,您总是至少经历一次循环。
first_pass = True
while first_pass or condition:
first_pass = False
do_stuff()
while condition or first_pass:
。然后 condition
总是首先被评估,整个 first_pass
只被评估两次(第一次和最后一次迭代)。不要忘记在循环之前将 condition
初始化为您想要的任何内容。
do {
stuff()
} while (condition())
->
while True:
stuff()
if not condition():
break
你可以做一个功能:
def do_while(stuff, condition):
while condition(stuff()):
pass
但是1)它很丑。 2) Condition 应该是一个带有一个参数的函数,应该由一些东西填充(这是不使用经典 while 循环的唯一原因。)
while True: stuff(); if not condition(): break
是一个非常好的主意。谢谢!
异常会打破循环,所以你不妨在循环外处理它。
try:
while True:
if s:
print s
s = i.next()
except StopIteration:
pass
我猜您的代码的问题是未定义 except
内的 break
的行为。通常 break
只向上一级,因此例如 try
内的 break
直接进入 finally
(如果存在)一个 try
外,但不是循环外。
相关 PEP:http://www.python.org/dev/peps/pep-3136
相关问题:Breaking out of nested loops
这是一个不同模式的更疯狂的解决方案——使用协程。代码仍然非常相似,但有一个重要区别;根本没有退出条件!当您停止向其提供数据时,协程(实际上是协程链)就会停止。
def coroutine(func):
"""Coroutine decorator
Coroutines must be started, advanced to their first "yield" point,
and this decorator does this automatically.
"""
def startcr(*ar, **kw):
cr = func(*ar, **kw)
cr.next()
return cr
return startcr
@coroutine
def collector(storage):
"""Act as "sink" and collect all sent in @storage"""
while True:
storage.append((yield))
@coroutine
def state_machine(sink):
""" .send() new parts to be tokenized by the state machine,
tokens are passed on to @sink
"""
s = ""
state = STATE_CODE
while True:
if state is STATE_CODE :
if "//" in s :
sink.send((TOKEN_COMMENT, s.split( "//" )[1] ))
state = STATE_COMMENT
else :
sink.send(( TOKEN_CODE, s ))
if state is STATE_COMMENT :
if "//" in s :
sink.send(( TOKEN_COMMENT, s.split( "//" )[1] ))
else
state = STATE_CODE
# re-evaluate same line
continue
s = (yield)
tokens = []
sm = state_machine(collector(tokens))
for piece in i:
sm.send(piece)
上面的代码将所有标记收集为 tokens
中的元组,我假设原始代码中的 .append()
和 .add()
之间没有区别。
我这样做的方式如下...
condition = True
while condition:
do_stuff()
condition = (<something that evaluates to True or False>)
在我看来,这似乎是一个简单的解决方案,我很惊讶我还没有在这里看到它。这显然也可以反转为
while not condition:
等等
我相信这种在 python 上的 do-while 模拟具有最接近 C 和 Java 中存在的 do-while 结构格式的语法格式。
do = True
while do:
[...]
do = <condition>
do = <condition>
?
do = <condition>
没有真正模拟 do ... while
循环
do ... while
循环总是无条件地运行第一次迭代,并且只在下一次迭代之前评估条件。
Python 3.8 给出了答案。
它被称为赋值表达式。来自 documentation:
# Loop over fixed length blocks
while (block := f.read(256)) != '':
process(block)
do
body while
condition 首先执行 body,然后评估 condition。您的构造首先检查条件。这是一段时间......做循环。
functools.partial()
和 iter()
:for block in iter(partial, file.read, 256), ""): process(block)
。
for 包含 try 语句的 do - while 循环
loop = True
while loop:
generic_stuff()
try:
questionable_stuff()
# to break from successful completion
# loop = False
except:
optional_stuff()
# to break from unsuccessful completion -
# the case referenced in the OP's question
loop = False
finally:
more_generic_stuff()
或者,当不需要“finally”子句时
while True:
generic_stuff()
try:
questionable_stuff()
# to break from successful completion
# break
except:
optional_stuff()
# to break from unsuccessful completion -
# the case referenced in the OP's question
break
while condition is True:
stuff()
else:
stuff()
stuff
是一个函数或重复代码主体。
while condition:
,因为 is True
是隐含的。
condition
依赖于 stuff()
的某个内部变量,则此操作将失败,因为此时未定义该变量。
快速破解:
def dowhile(func = None, condition = None):
if not func or not condition:
return
else:
func()
while condition():
func()
像这样使用:
>>> x = 10
>>> def f():
... global x
... x = x - 1
>>> def c():
global x
return x > 0
>>> dowhile(f, c)
>>> print x
0
你为什么不做
for s in l :
print s
print "done"
?
While循环:
while condition:
print("hello")
做while循环:
while True:
print("hello")
if not condition:
break
您也可以使用任何真正的布尔值作为条件:
while 1:
print("hello")
if not condition:
break
另一种变体:
check = 1
while check:
print("hello")
check = condition
如果您处于循环时资源不可用或类似的引发异常的情况,您可以使用类似的东西
import time
while True:
try:
f = open('some/path', 'r')
except IOError:
print('File could not be read. Retrying in 5 seconds')
time.sleep(5)
else:
break
你想知道:
我该怎么做才能捕获“停止迭代”异常并正确中断 while 循环?
您可以按如下所示执行此操作,并且还利用了 Python 3.8 中引入的 assignment expressions 功能(又名“海象运算符”):
list_of_ints = [1, 2, 3]
iterator = iter(list_of_ints)
try:
while (element := next(iterator)):
print(element)
except StopIteration:
print("done")
另一种可能性(适用于 Python 2.6 到 3.x)是为内置 next()
函数提供 default
参数以避免 StopIteration
异常:
SENTINEL = object() # Unique object.
list_of_ints = [1, 2, 3]
iterator = iter(list_of_ints)
while True:
element = next(iterator, SENTINEL)
if element is SENTINEL:
break
print(element)
print("done")
看看这是否有帮助:
在异常处理程序中设置一个标志并在处理 s 之前检查它。
flagBreak = false;
while True :
if flagBreak : break
if s :
print s
try :
s = i.next()
except StopIteration :
flagBreak = true
print "done"
while not flagBreak:
并删除 if (flagBreak) : break
来简化。
flag
的变量——我无法推断 True 值或 False 值的含义。请改为使用 done
或 endOfIteration
。代码变为 while not done: ...
。
对我来说,一个典型的 while 循环将是这样的:
xBool = True
# A counter to force a condition (eg. yCount = some integer value)
while xBool:
# set up the condition (eg. if yCount > 0):
(Do something)
yCount = yCount - 1
else:
# (condition is not met, set xBool False)
xBool = False
如果情况允许,我也可以在 while 循环中包含一个 for..loop,用于循环另一组条件。
while True:
try:
# stuff
stuff_1()
if some_cond:
continue
if other_cond:
break
stuff_2()
finally:
# condition
if not condition:
break
[x] 仅在运行后检查条件
[x] stuff 不是函数调用
[x] 条件不是函数调用
[x] 东西可以包含流控制
] 避免检查条件如果叫做 break 的东西(可以用另一个布尔值来完成)
内置 iter 函数专门执行以下操作:
for x in iter(YOUR_FN, TERM_VAL):
...
例如(在 Py2 和 3 中测试):
class Easy:
X = 0
@classmethod
def com(cls):
cls.X += 1
return cls.X
for x in iter(Easy.com, 10):
print(">>>", x)
如果要给出终止条件而不是值,则始终可以设置相等,并要求相等为 True
。
不定期副业成功案例分享