Python 如何在for循环中倒计时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29292133/
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
How to count down in for loop?
提问by Jika
In Java, I have the following for loop and I am learning Python:
在 Java 中,我有以下 for 循环,我正在学习 Python:
for (int index = last-1; index >= posn; index--)
My question is simple and probably obvious for most of people who are familiar with Python. I would like to code that 'for' loop in Python. How can I do this?
我的问题很简单,对于大多数熟悉 Python 的人来说可能很明显。我想在 Python 中编写“for”循环。我怎样才能做到这一点?
I tried to do the following:
我尝试执行以下操作:
for index in range(last-1, posn, -1):
I think it should be range(last-1, posn + 1, -1)
. Am I right?
我觉得应该是range(last-1, posn + 1, -1)
。我对吗?
I am thankful to anyone especially who will explain to me how to understand the indices work in Python.
我感谢任何人,尤其是那些会向我解释如何理解 Python 中的索引工作的人。
采纳答案by Loocid
The range function in python has the syntax:
python 中的 range 函数的语法如下:
range(start, end, step)
range(start, end, step)
It has the same syntax as python lists where the start is inclusive but the end is exclusive.
它与 python 列表的语法相同,其中开始包含但结尾不包含。
So if you want to count from 5 to 1, you would use range(5,0,-1)
and if you wanted to count from last
to posn
you would use range(last, posn - 1, -1)
.
所以,如果你想从5数到1,你可以使用range(5,0,-1)
,如果你想从数last
到posn
你会使用range(last, posn - 1, -1)
。
回答by Izaac Corbett
If you google. "Count down for loop python" you get these, which are pretty accurate.
如果你谷歌。“倒计时循环python”你得到这些,这是非常准确的。
how to loop down in python list (countdown)
Loop backwards using indices in Python?
如何在 Python 列表中向下循环(倒计时)
使用 Python 中的索引向后循环?
I recommend doing minor searches before posting. Also "Learn Python The Hard Way" is a good place to start.
我建议在发帖前进行少量搜索。“Learn Python The Hard Way”也是一个很好的起点。
回答by Burger King
First I recommand you can try use print and observe the action:
首先我建议您可以尝试使用打印并观察操作:
for i in range(0, 5, 1):
print i
the result:
结果:
0
1
2
3
4
You can understand the function principle.
In fact, range
scan range is from 0
to 5-1
.
It equals 0 <= i < 5
可以理解功能原理。实际上,range
扫描范围是从0
到5-1
。它等于0 <= i < 5
When you really understand for-loop in python, I think its time we get back to business. Let's focus your problem.
当您真正了解 Python 中的 for 循环时,我认为是时候回到正题了。让我们聚焦您的问题。
You want to use a DECREMENT for-loop in python. I suggest a for-loop tutorial for example.
您想在 python 中使用 DECREMENT for 循环。例如,我建议使用 for-loop 教程。
for i in range(5, 0, -1):
print i
the result:
结果:
5
4
3
2
1
Thus it can be seen, it equals 5 >= i > 0
由此可见,它等于 5 >= i > 0
You want to implement your java code in python:
你想在python中实现你的java代码:
for (int index = last-1; index >= posn; index--)
It should code this:
它应该这样编码:
for i in range(last-1, posn-1, -1)
回答by chapelo
In python, when you have an iterable, usually you iterate without an index:
在 python 中,当你有一个可迭代对象时,通常你会在没有索引的情况下进行迭代:
letters = 'abcdef' # or a list, tupple or other iterable
for l in letters:
print(l)
If you need to traverse the iterable in reverse order, you would do:
如果您需要以相反的顺序遍历可迭代对象,您可以这样做:
for l in letters[::-1]:
print(l)
When for any reason you need the index, you can use enumerate
:
当出于任何原因需要索引时,您可以使用enumerate
:
for i, l in enumerate(letters, start=1): #start is 0 by default
print(i,l)
You can enumerate in reverse order too...
您也可以以相反的顺序枚举...
for i, l in enumerate(letters[::-1])
print(i,l)
ON ANOTHER NOTE...
另一个注意事项...
Usually when we traverse an iterable we do it to apply the same procedure or function to each element. In these cases, it is better to use map
:
通常,当我们遍历一个可迭代对象时,我们会对每个元素应用相同的过程或函数。在这些情况下,最好使用map
:
If we need to capitilize each letter:
如果我们需要将每个字母大写:
map(str.upper, letters)
Or get the Unicode code of each letter:
或者获取每个字母的Unicode代码:
map(ord, letters)