Python for 或 while 循环做 n 次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17647907/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:46:43  来源:igfitidea点击:

for or while loop to do something n times

pythonperformanceloopsfor-loopwhile-loop

提问by Sventimir

In Python you have two fine ways to repeat some action more than once. One of them is whileloop and the other - forloop. So let's have a look on two simple pieces of code:

在 Python 中,您有两种很好的方法可以多次重复某些操作。其中一个是while循环,另一个是for循环。那么让我们看一下两段简单的代码:

for i in range(n):
    do_sth()

And the other:

和另一个:

i = 0
while i < n:
    do_sth()
    i += 1

My question is which of them is better. Of course, the first one, which is very common in documentation examples and various pieces of code you could find around the Internet, is much more elegant and shorter, but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?

我的问题是它们中的哪一个更好。当然,第一个在文档示例和您可以在 Internet 上找到的各种代码片段中非常常见,它更优雅和更短,但另一方面,它创建了一个完全无用的整数列表,只是为了循环他们。这不是浪费内存,尤其是就大量迭代而言?

So what do you think, which way is better?

那么你怎么看,哪种方式更好?

采纳答案by Amber

but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?

但另一方面,它创建了一个完全无用的整数列表,只是为了循环它们。这不是浪费内存,尤其是就大量迭代而言?

That is what xrange(n)is for. It avoids creating a list of numbers, and instead just provides an iterator object.

xrange(n)是为了什么。它避免创建数字列表,而是只提供一个迭代器对象。

In Python 3, xrange()was renamed to range()- if you want a list, you have to specifically request it via list(range(n)).

在 Python 3 中,xrange()被重命名为range()- 如果你想要一个列表,你必须通过list(range(n)).

回答by John La Rooy

This is lighter weight than xrange(and the while loop) since it doesn't even need to create the intobjects. It also works equally well in Python2 and Python3

这比xrange(和 while 循环)更轻,因为它甚至不需要创建int对象。它在 Python2 和 Python3 中也同样有效

from itertools import repeat
for i in repeat(None, 10):
    do_sth()

回答by Steve Barnes

The fundamental difference in most programming languages is that unlessthe unexpected happens a forloop will alwaysrepeat ntimes then finish with a whileloop it may repeat 0 times, 1, more or even forever, depending on a given condition which is always true at the start of each loop and always false on exiting the loop, (for completeness a do ... whileloop, (or repeat until), for languages that have it, always executes at least once and does not guarantee the condition on the first execution).

大多数编程语言的根本区别在于,除非发生意外,否则for循环将始终重复n多次,然后以while可能重复的循环结束0 times, 1, more or even forever,这取决于给定的条件,该条件在每个循环开始时始终为真,而在退出循环时始终为假。 , (为了完整性,do ... while循环,(或repeat until),对于有它的语言,总是至少执行一次并且不保证第一次执行时的条件)。

So the answer to your question is 'it all depends on what you are trying to do'!

所以你的问题的答案是'这一切都取决于你想要做什么'!