Python中循环迭代的重做
Python是否有一些语言中存在的“redo”语句Python中循环迭代的重做,python,python-2.7,for-loop,redo,Python,Python 2.7,For Loop,Redo,Python是否有一些语言中存在的“redo”语句 (redo语句是一种影响循环行为的语句(就像“break”或“continue”)它从最内层循环的开始处跳转,然后再次开始执行。)不,它没有。我建议使用while循环并将check变量重置为初始值 count = 0 reset = 0 while count < 9: print 'The count is:', count if not someResetCondition: count = count +
(redo语句是一种影响循环行为的语句(就像“break”或“continue”)它从最内层循环的开始处跳转,然后再次开始执行。)不,它没有。我建议使用while循环并将check变量重置为初始值
count = 0
reset = 0
while count < 9:
print 'The count is:', count
if not someResetCondition:
count = count + 1
count=0
重置=0
当计数小于9时:
打印“计数为:”,计数
如果不满足以下条件:
计数=计数+1
不,Python不直接支持redo
。其中一个选项是涉及嵌套循环的稍微可怕的事情,如:
for x in mylist:
while True:
...
if shouldredo:
continue # continue becomes equivalent to redo
...
if shouldcontinue:
break # break now equivalent to continue on outer "real" loop
...
break # Terminate inner loop any time we don't redo
但这意味着在“redo
-able”块中,如果不诉诸异常、标记变量或将整个内容打包为函数,就不可能中断外部循环
或者,您可以使用一个直接的while
循环来复制for
循环为您所做的事情,显式地创建并推进迭代器。它有自己的问题(continue
实际上是redo
默认情况下,您必须显式地将迭代器提前到“real”continue
),但它们并不可怕(只要您对continue
的用法进行注释,以明确您打算redo
与continue
的对比,以避免混淆维护人员)。要允许redo
和其他循环操作,您可以执行以下操作:
# Create guaranteed unique sentinel (can't use None since iterator might produce None)
sentinel = object()
iterobj = iter(mylist) # Explicitly get iterator from iterable (for does this implicitly)
x = next(iterobj, sentinel) # Get next object or sentinel
while x is not sentinel: # Keep going until we exhaust iterator
...
if shouldredo:
continue
...
if shouldcontinue:
x = next(iterobj, sentinel) # Explicitly advance loop for continue case
continue
...
if shouldbreak:
break
...
# Advance loop
x = next(iterobj, sentinel)
上述操作也可以通过
try
/完成,除了StopIteration:
而不是使用sentinel
的两个argnext
,但是用它包装整个循环有可能捕获到StopIteration
的其他源,并在有限的范围内正确地执行内部和外部next
调用这将非常难看(比基于sentinel
的方法糟糕得多)。这是我使用迭代器的解决方案:
class redo_iter(object):
def __init__(self, iterable):
self.__iterator = iter(iterable)
self.__started = False
self.__redo = False
self.__last = None
self.__redone = 0
def __iter__(self):
return self
def redo(self):
self.__redo = True
@property
def redone(self):
return self.__redone
def __next__(self):
if not (self.__started and self.__redo):
self.__started = True
self.__redone = 0
self.__last = next(self.__iterator)
else:
self.__redone += 1
self.__redo = False
return self.__last
# Display numbers 0-9.
# Display 0,3,6,9 doubled.
# After a series of equal numbers print --
iterator = redo_iter(range(10))
for i in iterator:
print(i)
if not iterator.redone and i % 3 == 0:
iterator.redo()
continue
print('---')
- 需要明确的
继续
是一项额外功能重做
- 对于Python2,使用
而不是def next(self)
def\uuuuuuuuuuuuuuuuuuuuuuu(self)
- 需要在循环之前定义
迭代器
my @words = qw(fred barney pebbles dino wilma betty);
my $error = 0;
my @words = qw(fred barney pebbles dino wilma betty);
my $error = 0;
foreach (@words){
print "Type the word '$_':";
chomp(my $try = <STDIN>);
if ($try ne $_){
print "Sorry - That's not right.\n\n";
$error++;
redo;
}
}
Python没有“重做”功能语法,但我们可以在某些函数中进行“while”循环,直到在输入列表时得到我们想要的结果。不是很复杂,但很容易阅读,使用
while和循环末尾的增量。因此,任何continue
之间的循环都会产生重做的效果。每3次重复一次示例:
redo = True # To ends redo condition in this sample only
i = 0
while i<10:
print(i, end='')
if redo and i % 3 == 0:
redo = False # To not loop indifinively in this sample
continue # Redo
redo = True
i += 1
redo=True#仅结束此示例中的重做条件
i=0
而python中没有重做。
一个非常容易理解的解决方案如下:
for x in mylist:
redo = True
while redo:
redo = False
If should_redo:
redo = True
这很清楚,不需要添加评论
Continue
将像在for循环中一样工作
但是break
不可用,这使得break
可用,但代码不太清晰。有很多方法可以做到这一点。首先,你可以在的同时使用循环并重置你的计数器/条件。从未听说过这样的事情。听起来很像goto@ChristopherSchneider:Perl使用它(为了它的价值)。考虑一个不执行循环推进步骤的继续
。因为它与循环本身相关联,所以它在道德上与继续和中断没有什么区别;如果你接受它们,而不仅仅是转到,那么重做就不会更糟(或更好)。使用reset
不会复制redo
在其他语言中所做的操作。redo
是continue
没有循环增量/前进步骤,但它不会从一开始就重新启动循环;您只需将计数
增量设置为可选,而没有reset
变量。啊,我误读了您的初始化ial声明“从一开始就跳”意思是初始点,而不仅仅是循环的顶部。我将修改我的答案。我不是OP,这是他们的初始语句,不是我的。我承认我可能是误读了,但我所知道的唯一一种使用redo
的语言是Perl,它的行为方式是这样的。注意:如果要将替换为范围内的计数(10),则编辑的代码就可以了:
,但它并不是特别适用于任意的可编辑性;我在中的第二个代码示例是完全通用的版本。这应该是公认的答案。重做不会很像pythonic,如果需要,模仿for循环和标记to redo将很容易为其他人所理解。谢谢
for x in mylist:
redo = True
while redo:
redo = False
If should_redo:
redo = True