如何模拟 do-while 循环?
- 2024-12-19 09:24:00
- admin 原创
- 138
问题描述:
我需要在 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
解决方案 1:
我不确定你想做什么。你可以像这样实现 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
解决方案 2:
这是一个模拟 do-while 循环的非常简单的方法:
condition = True
while condition:
# loop body here
condition = test_loop_condition()
# end of loop
do-while 循环的主要特点是循环体始终至少执行一次,并且条件在循环体底部进行评估。此处显示的控制结构无需异常或 break 语句即可实现这两个功能。它确实引入了一个额外的布尔变量。
解决方案 3:
下面的代码可能是一个有用的实现,突出了两者之间的主要区别做而对比尽管据我了解。
因此,在这种情况下,您总是至少要经历一次循环。
first_pass = True
while first_pass or condition:
first_pass = False
do_stuff()
解决方案 4:
do {
stuff()
} while (condition())
->
while True:
stuff()
if not condition():
break
你可以做一个功能:
def do_while(stuff, condition):
while condition(stuff()):
pass
但是 1) 它很丑陋。2)条件应该是一个具有一个参数的函数,应该由东西填充(这是不使用经典 while 循环的唯一原因。)
解决方案 5:
异常会破坏循环,因此你最好在循环外处理它。
try:
while True:
if s:
print s
s = i.next()
except StopIteration:
pass
我猜你的代码的问题在于break
内部的行为except
没有定义。通常break
只向上一级,因此例如break
内部try
直接进入finally
(如果存在)外部try
,但不会离开循环。
相关 PEP:http://www.python.org/dev/peps/pep-3136
相关问题:跳出嵌套循环
解决方案 6:
我这样做的方式如下......
condition = True
while condition:
do_stuff()
condition = (<something that evaluates to True or False>)
在我看来,这是最简单的解决方案,我很惊讶我还没有在这里看到它。这显然也可以反转为
while not condition:
ETC。
解决方案 7:
我认为,python 上的这种 do-while 模拟的语法格式与 C 和 Java 中的 do-while 结构格式最为接近。
do = True
while do:
[...]
do = <condition>
解决方案 8:
这是另一种模式的更疯狂的解决方案——使用协程。代码仍然非常相似,但有一个重要的区别;根本没有退出条件!当您停止向协程(实际上是协程链)提供数据时,协程就会停止。
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()`
解决方案 9:
Python 3.8 给出了答案。
这被称为赋值表达式。摘自文档:
# Loop over fixed length blocks
while (block := f.read(256)) != '':
process(block)
解决方案 10:
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
解决方案 11:
对于包含 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
解决方案 12:
快速破解:
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
解决方案 13:
你为什么不这样做
for s in l :
print s
print "done"
?
解决方案 14:
while condition is True:
stuff()
else:
stuff()
解决方案 15:
你想知道:
我该怎么做才能捕获“停止迭代”异常并正确中断 while 循环?
您可以按如下所示进行操作,同时还利用了Python 3.8 中引入的赋值表达式功能(又名“海象运算符”):
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)是向default
内置next()
函数提供一个参数以避免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")
解决方案 16:
您可以使用 Python 3.8 中的海象运算符稍微清理一下:
list_of_ints = [ 1, 2, None, 3 ]
iterator = iter(list_of_ints)
_sentinel = object()
while True:
if (i := next(iterator, _sentinel)) is _sentinel:
break
print(i)
这在 之外没有重复的逻辑while
。它还处理列表中求值为 的值False
。
解决方案 17:
基于evan54的回答:
定义辅助类
class FirstAccess:
def __init__(self):
self._accessed = False
def __bool__(self):
if self._accessed:
return False
self._accessed = True
return True
可以将标志的更新保存在循环体中,因为通过访问标志可以进行更新:
first_pass = FirstAccess()
while first_pass or condition:
do_stuff()
解决方案 18:
看看这是否有帮助:
在异常处理程序中设置一个标志,并在处理 s 之前检查它。
flagBreak = false;
while True :
if flagBreak : break
if s :
print s
try :
s = i.next()
except StopIteration :
flagBreak = true
print "done"
解决方案 19:
如果你处于循环过程中,而资源不可用或出现类似情况并引发异常,则可以使用类似
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
解决方案 20:
对于我来说,典型的 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 ,以循环遍历另一组条件。
解决方案 21:
while True:
try:
# stuff
stuff_1()
if some_cond:
continue
if other_cond:
break
stuff_2()
finally:
# condition
if not condition:
break
[x]
condition
仅在运行后检查stuff
[x]
stuff
不是函数调用[x]
condition
不是函数调用[x]
stuff
可以包含流量控制[ ] 避免检查
condition
是否stuff
被调用break
(可以用另一个布尔值来完成)
解决方案 22:
内置的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
。