Python 在 for 循环中移回迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14374181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving back an iteration in a for loop
提问by hemlocker
So I want to do something like this:
所以我想做这样的事情:
for i in range(5):
print(i);
if(condition==true):
i=i-1;
However, for whatever reason, even though I'm decrementing i, the loop doesn't seem to notice. Is there any way to repeat an iteration?
但是,无论出于何种原因,即使我正在减少 i,循环似乎也没有注意到。有没有办法重复迭代?
采纳答案by Gabe
forloops in Python always go forward. If you want to be able to move backwards, you must use a different mechanism, such as while:
forPython 中的循环总是向前进行。如果您希望能够向后移动,则必须使用不同的机制,例如while:
i = 0
while i < 5:
print(i)
if condition:
i=i-1
i += 1
Or even better:
或者甚至更好:
i = 0
while i < 5:
print(i)
if condition:
do_something()
# don't increment here, so we stay on the same value for i
else:
# only increment in the case where we're not "moving backwards"
i += 1
回答by wim
You have a misunderstanding about loops in Python. The forloop doesn't care what you do with iat each iteration, because it is not related to the logic of the loop at all. Modifying ijust rebinds a local variable.
您对 Python 中的循环有误解。该for循环不关心你做什么i,在每一次迭代,因为它是不相关的所有循环的逻辑。修改i只是重新绑定一个局部变量。
You would need to use a while loop to achieve the behaviour you're expecting, where the state of idoes affect the control flow of the loop:
您需要使用 while 循环来实现您期望的行为,其中的状态i确实会影响循环的控制流:
import random
i = 0
while i < 5:
print(i)
i += 1
if random.choice([True, False]):
i -= 1
回答by Thorsten Kranz
Python loop using rangeare by-design to be different from C/C++/Java for-loops. For every iteration, the i is set the the next value of range(5), no matter what you do to iin between.
Python 循环的使用range是for特意设计的,与 C/C++/Java循环不同。对于每次迭代, i 被设置为 的下一个值range(5),无论您i在两者之间做什么。
You could use a while-loop instead:
您可以改用 while 循环:
i = 0
while i<5:
print i
if condition:
continue
i+=1
But honestly: I'd step back and think again about your original problem. Probably you'll find a better solution as such loops are always error-prone. There's a reason why Python for-loops where designed to be different.
但老实说:我会退后一步,再想想你最初的问题。您可能会找到更好的解决方案,因为此类循环总是容易出错。Python for-loops 的设计与众不同是有原因的。
回答by Yuushi
Utilize a whileloop:
使用while循环:
i = 0
while i < 5:
print(i)
if condition:
i -= 1
i += 1
As has been mentioned, this is rather unidiomatic Python. Perhaps if you post what you are trying to achieve we can give some better advice.
如前所述,这是相当单调的 Python。也许如果您发布您想要实现的目标,我们可以提供一些更好的建议。
回答by elssar
range(5)creates a list with numbers 0thru 4in it - [0, 1, 2, 3, 4].
range(5)创建一个带有编号的列表0通4在里面- [0, 1, 2, 3, 4]。
When you run a for loop over it, you are iterating over the list. Doing i-= 1will only decrement the valueof that particular element of the list, and the iteration will continue.
当您对其运行 for 循环时,您正在遍历列表。这样做i-= 1只会减少列表中该特定元素的值,并且迭代将继续。
Like the other answers here have suggested, you should use a whileloop.
就像这里的其他答案所建议的那样,您应该使用while循环。
i= 0
while i<5:
# do stuff
if #condition:
i-= 1 # or +
i+= 1
回答by Snakes and Coffee
Repeating many other answers, and just for completeness, you will need to use a while loop.
重复许多其他答案,为了完整起见,您需要使用while loop.
i = 0
while i < 5:
print(i)
if (not condition):
i+=1
If you want to move back an iteration in the loop (instead of repeating an iteration), then use this:
如果要在循环中移回迭代(而不是重复迭代),请使用以下命令:
i = 0
while i < 5:
print(i)
if (condition):
i -= 1
else:
i += 1
Essentially, while i < 5evaluates upon each iteration, and checks if i < 5. Thus by decrementing/not changing i, we get something like this: (values of i)
本质上,while i < 5在每次迭代时进行评估,并检查是否i < 5。因此,通过递减/不改变i,我们得到这样的结果:(的值i)
Not changing: 1->2->3-(condition satisfied)> 3 -> 4 -> 5
不变: 1->2->3-(condition satisfied)> 3 -> 4 -> 5
Decrementing: 1->2->3-(condition satisfied)>2 -> 3 -> 4 -> 5
递减: 1->2->3-(condition satisfied)>2 -> 3 -> 4 -> 5
The reason why i=i-1in your for loop doesn't make it repeat the iteration is simple. In the forloop, iis assigned the value of the next item in the for loop. Python could care less about what you do with i, as long as it is able to assign the next item to it. Thus, the for loop for i in <your_iterable>:<do whatever>is closer to this:
之所以i=i-1在你的循环不会让它重复迭代很简单。在for循环中,i被赋值为 for 循环中下一项的值。只要 Pythoni能够将下一项分配给它,它就不会关心你用 做什么。因此,for 循环for i in <your_iterable>:<do whatever>更接近于这个:
_i = 0
_length = len(<your_iterable>)
while _i < _length:
i = _i
_i += 1
<do whatever>
However, in this analogy, you wouldn't be able to access the _predicated variables (_i,_length). This is how I simplify the logic of the for loop. Note that regardless of what iis assigned to, it will be assigned to _iupon the next iteration, and the loop really doesn't care about what iis.
但是,在这个类比中,您将无法访问_谓词变量 ( _i, _length)。这就是我简化for loop. 请注意,无论i分配给什么,它都会_i在下一次迭代时分配给,并且循环实际上并不关心是什么i。
回答by georg
In Python it's possible to set up a two-way exchange between an iterator (what comes after inin a for..inloop) and its consumer (code inside the loop). To achieve this, you can use sendin the consumer code to "inject" a value in a generator. In your case, you can simply send back the current value once the condition is met and wrap the rangecall in a generator that repeats whatever is sent back to it. Here's some code for you to play, intentionally verbose for clarity:
在Python有可能建立一个迭代器之间的双向交流(后随之而来in的for..in循环)和它的消费者(内循环代码)。为此,您可以send在消费者代码中使用在生成器中“注入”一个值。在您的情况下,您可以在满足条件后简单地发回当前值,并将range调用包装在一个生成器中,该生成器重复发送回的任何内容。这里有一些代码供您玩,为了清楚起见,特意详细说明:
def repeateble(it):
buf, it = None, iter(it)
while True:
if buf is None:
# the buffer is empty, send them the next elem
val = next(it)
else:
# there's something in the buffer
# let's send that back
val = buf
# send the value and wait what they say
back = yield val
if back:
# they've sent us something!
# give them some dummy value as a result of send()
yield None
# and save what they've sent in a buffer
# for the next iteration
buf = back
else:
# they haven't sent anything
# empty the buffer
buf = None
from random import randint
# create a repeateble generator
rng = repeateble(range(100))
for x in rng:
print(x)
# check "some condition"...
if randint(1, 100) > 80:
print('repeat:')
# send the current value back to the generator
# it will be returned on the next iteration
rng.send(x)
回答by user11494643
You can use readlines if you're iterating through a file and pull out the previous lines based on a condition.
如果您正在遍历文件并根据条件拉出前几行,则可以使用 readlines。
with open("myfile.txt", "r") as f:
text = f.readlines()
for row in range(0, len(text)):
if re.search("Error", text[row]):
print(text[row-1].strip())

