循环向后 [英] Loop Backwards
问题描述
这应该很简单,但我无法理解:
如何在列表中向后循环?
例如,在javascript 中:
for(var i = list.length - 1; i> = 0; i--){
do_stuff()
}
我的意思是,我可以撤销列表,但我不想。我希望它能够保持完全相同,但我想在结束时开始并以
开头结束。
谢谢!
- 戴夫
Dave写道:这应该很简单,但我不能理解:
如何在列表中向后循环?
例如,在javascript :
for(var i = list.length - 1; i> = 0; i--){
do_stuff()
}
我的意思是,我可以撤销列表,但我不想这样做。我希望它保持完全相同,但我想从最后开始并在
开头结束。
负指数的起点与正指数相反。
s = [1,2,3,4,5]
我在范围内(len(s)):
print s [-i-1],
5 4 3 2 1
谢谢!
- 戴夫
谢谢!我知道这很简单...
Dave写道:这应该很简单,但我不能得到它:
如何在列表中向后循环?
例如,在javascript :
> for(var i = list.length - 1; i> = 0; i--){
do_stuff()
}
我的意思是,我可以扭转列表,但我不想。我希望它保持完全相同,但我想从最后开始并在
开头结束。
谢谢!
- 戴夫
for i in revers(ls):
do_stuff()
reverse doesn不要反转清单。它返回一个迭代器,通过列表向后迭代
。
干杯,
Carl。
This should be simple, but I can''t get it:
How do you loop backwards through a list?
For example, in, say, javascript:
for (var i = list.length - 1; i >=0; i--) {
do_stuff()
}
I mean, I could reverse the list, but I don''t want to. I want it to
stay exactly the same, but I want to start at the end and end at the
beginning.
Thanks!
- Dave
Dave wrote:This should be simple, but I can''t get it:
How do you loop backwards through a list?
For example, in, say, javascript:
for (var i = list.length - 1; i >=0; i--) {
do_stuff()
}
I mean, I could reverse the list, but I don''t want to. I want it to
stay exactly the same, but I want to start at the end and end at the
beginning.
Negative indexes start at the opposite end than positive indexes.
s = [1,2,3,4,5]
for i in range(len(s)): print s[-i-1],
5 4 3 2 1
Thanks!
- Dave
Thanks! I knew it was simple...
Dave wrote:This should be simple, but I can''t get it:
How do you loop backwards through a list?
For example, in, say, javascript:
for (var i = list.length - 1; i >=0; i--) {
do_stuff()
}
I mean, I could reverse the list, but I don''t want to. I want it to
stay exactly the same, but I want to start at the end and end at the
beginning.
Thanks!
- Dave
for i in reversed(ls):
do_stuff()
reversed doesn''t reverse the list. It returns an iterator that iterates
through the list backwards.
Cheers,
Carl.
这篇关于循环向后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!