Python - 重新启动 for 循环的方法,类似于“继续” for while 循环?
基本上,我需要一种方法将控制权返回到 for 循环的开头,并且在满足特定条件时采取操作后实际上重新启动整个迭代过程。
我想做的是这样的:
for index, item in enumerate(list2):
if item == '||' and list2[index-1] == '||':
del list2[index]
*<some action that resarts the whole process>*
这样,如果 ['berry','||','||','||','pancake] 在列表中,我最终会得到:
[ '浆果','||','煎饼'] 代替。
谢谢!
Basically, I need a way to return control to the beginning of a for loop and actually restart the entire iteration process after taking an action if a certain condition is met.
What I'm trying to do is this:
for index, item in enumerate(list2):
if item == '||' and list2[index-1] == '||':
del list2[index]
*<some action that resarts the whole process>*
That way, if ['berry','||','||','||','pancake] is inside the list, I'll wind up with:
['berry','||','pancake'] instead.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我不确定你所说的“重新启动”是什么意思。您想从头开始迭代,还是直接跳过当前迭代?
如果是后者,那么
for
循环支持continue
就像while
循环一样:上面将打印从 0 到 9 的数字,除了5.
如果您正在讨论从
for
循环的开头重新开始,那么除了“手动”之外没有其他办法,例如将其包装在while
中> 循环:上面将打印从 0 到 5 的数字,然后再次从 0 开始,等等(我知道这不是一个很好的例子)。
I'm not sure what you mean by "restarting". Do you want to start iterating over from the beginning, or simply skip the current iteration?
If it's the latter, then
for
loops supportcontinue
just likewhile
loops do:The above will print the numbers from 0 to 9, except for 5.
If you're talking about starting over from the beginning of the
for
loop, there's no way to do that except "manually", for example by wrapping it in awhile
loop:The above will print the numbers from 0 to 5, then start over from 0 again, and so on indefinitely (not really a great example, I know).
这会做你想要的事情。为什么你想要这样做是另一回事。也许您应该看一下您的代码,并确保您没有错过明显且更简单的方法。
That will do what you seem to want. Why you would want to do it is a different matter. Maybe you should take a look at your code and make sure you're not missing an obvious and easier way to do it.
一种糟糕的算法思考方式。
您只是进行过滤,即删除重复项。
而且 - 在 Python 中 - 你最高兴的是制作副本,而不是尝试执行
del
。一般来说,很少有调用使用del
。A poor way to think of an algorithm.
You're just filtering, i.e., removing duplicates.
And -- in Python -- you're happiest making copies, not trying to do
del
. In general, there's very little call to usedel
.不可避免的 itertools 版本,因为它刚刚出现在我面前:
The inevitable itertools version, because it just came to me:
Continue
适用于任何循环。Continue
will work for any loop.continue
也适用于 for 循环。continue
works in for loops also.正如您所看到的,回答您的问题会导致一些相当复杂的代码。通常可以找到更好的方法,这就是为什么此类构造没有内置到语言中的原因。
如果您不习惯使用 itertools,请考虑使用此循环。它不仅比重新启动 for 循环更容易遵循,而且效率更高,因为它不会浪费时间重新检查已经跳过的项目。
As you can see answering your question leads to some rather convoluted code. Usually a better way can be found, which is why such constructs aren't built into the language
If you are not comfortable using itertools, consider using this loop instead. Not only is it easier to follow than your restarting for loop, it is also more efficient because it doesn't waste time rechecking items that have already been passed over.
顺便说一句,这是重复 从 a 中删除相邻的重复元素列表
And by the way this is repeating of Remove adjacent duplicate elements from a list