在python中使用条件循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4323946/
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
Loop with conditions in python
提问by Boris Gorelik
Consider the following code in C:
考虑以下 C 代码:
for(int i=0; i<10 && some_condition; ++i){
do_something();
}
I would like to write something similar in Python. The best version I can think of is:
我想用 Python 写一些类似的东西。我能想到的最好的版本是:
i = 0
while some_condition and i<10:
do_something()
i+=1
Frankly, I don't like whileloops that imitate forloops. This is due to the risk of forgetting to increment the counter variable. Another option, that addressess this risk is:
坦率地说,我不喜欢while模仿for循环的循环。这是由于忘记增加计数器变量的风险。解决此风险的另一种选择是:
for i in range(10):
if not some_condition: break
do_something()
Important clarifications
重要说明
some_conditionis not meant to be calculated during the loop, but rather to specify whether to start the loop in the first placeI'm referring to Python2.6
some_condition不是在循环期间计算,而是首先指定是否开始循环我指的是 Python2.6
Which style is preferred? Is there a better idiom to do this?
哪种风格更受欢迎?有没有更好的成语来做到这一点?
采纳答案by Karl Knechtel
In general, the "range+ break" style is preferred - but in Python 2.x, use xrangeinstead of rangefor iteration (this creates the values on-demand instead of actually making a list of numbers).
通常,首选“ range+ break”样式 - 但在 Python 2.x 中,使用xrange代替range迭代(这会根据需要创建值,而不是实际制作数字列表)。
But it always depends. What's special about the number 10 in this context? What exactly is some_condition? Etc.
但这总是取决于。在这种情况下,数字 10 有什么特别之处?究竟是some_condition什么?等等。
Response to update:
响应更新:
It sounds as though some_conditionis a "loop invariant", i.e. will not change during the loop. In that case, we should just test it first:
听起来好像some_condition是“循环不变式”,即在循环期间不会改变。在这种情况下,我们应该先测试一下:
if some_condition:
for i in xrange(10):
do_something()
回答by John Machin
forloops with a constant upper bound are a bit rare in Python. If you are iterating over somearray, you might do:
for具有恒定上限的循环在 Python 中很少见。如果您正在迭代somearray,您可能会这样做:
for i in xrange(len(somearray)):
if not some_condition:
break
do_sth_with(i, somearray[i])
or, better:
或更好:
for i, item in enumerate(somearray):
if not some_condition:
break
do_sth_with(i, item)
回答by Martin Tóth
This might not be related, but there's what I'm used to do... If some_conditionis simple enough, put it in a function and filteritems you iterate over:
这可能不相关,但这是我习惯做的......如果some_condition足够简单,将它放在一个函数和filter你迭代的项目中:
def some_condition(element):
return True#False
for i in filter(some_condition, xrange(10)):
pass
You can use this approach also when you iterate over some list of elements.
当您迭代某些元素列表时,您也可以使用这种方法。
selected = filter(some_condition, to_process)
for i, item in enumerate(selected):
pass
Again, this might not be your case, you should choose method of filtering items depending on your problem.
同样,这可能不是您的情况,您应该根据您的问题选择过滤项目的方法。

