Python for循环递减索引

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

Python for loop decrementing index

pythonpython-3.4

提问by BVBC

So I wrote a for loop like this:

所以我写了一个这样的for循环:

for i in range(size):
  if(.....)
    ....
    i-=1
  else:
    ....

I try to decrease the index by 1 if it is inside the ifstatement, but apparently I can't do that. Is there any other way that I can decrease iin a for loop?

如果它在if语句中,我尝试将索引减少 1 ,但显然我不能这样做。有没有其他方法可以减少ifor 循环?

采纳答案by Dranithix

I would like to go over the range() function yet again through the documentation as provided here: Python 3.4.1 Documentation for range(start, stop[, step])

我想通过此处提供的文档再次查看 range() 函数:Python 3.4.1 Documentation for range(start, stop[, step])

As shown in the documentation above, you may enter three parameters for the range function 'start', 'stop', and 'step', and in the end it will give you an immutable sequence.

如上面的文档所示,您可以为范围函数 'start'、'stop' 和 'step' 输入三个参数,最终它会给您一个不可变的序列

The 'start' parameter defines when the counter variable for your case 'i' should begin at. The 'end' parameter is essentially what the size parameter does in your case. And as for what you want, being that you wish to decrease the variable 'i' by 1 every loop, you can have the parameter 'step' be -1 which signifies that on each iteration of the for loop, the variable 'i' will go down by 1.

'start' 参数定义您的案例 'i' 的计数器变量应该从何时开始。'end' 参数本质上是 size 参数在您的情况下所做的。至于您想要什么,因为您希望每次循环将变量“i”减少 1,您可以将参数“step”设为 -1,这表示在 for 循环的每次迭代中,变量“i”将下降 1。

You can set the 'step' to be -2 or -4 as well, which would make the for loop count 'i' down on every increment 2 down or 4 down respectively.

您也可以将 'step' 设置为 -2 或 -4,这将使 for 循环在每个增量 2 向下或 4 向下时计数为 'i'。

Example:

例子:

for x in range(9, 3, -3):
    print(x)

Prints out: 9, 6. It starts at 9, ends at 3, and steps down by a counter of 3. By the time it reaches 3, it will stop and hence why '3' itself is not printed.

打印出:9, 6。它从 9 开始,在 3 结束,并以 3 的计数器递减。当它达到 3 时,它将停止,因此为什么不打印 '3' 本身。

EDIT:Just noticed the fact that it appears you may want to decrease the 'i' value in the for loop...? What I would do for that then is simply have a while loop instead where you would have a variable exposed that you can modify at your whim.

编辑:刚刚注意到您可能想要减少 for 循环中的 'i' 值...?然后我会做的只是有一个 while 循环,而不是在那里你会暴露一个变量,你可以随心所欲地修改它。

test = 0
size = 50
while (test < size)
    test += 1
    if (some condition):
        test -= 1

回答by Dair

You should not mutate i. The for i in range(size):is kind of special. Instead you should probably use a list comprehension:

你不应该变异i。该for i in range(size):是一种特殊的。相反,您可能应该使用列表理解:

size_range = [x-1 if condition else x for x in range(size)]

Actually probably better:

实际上可能更好:

def f(x):
    ... #Do else stuff here.

decrement = lambda x: x-1
size_range = [decrement(x) if condition else f(x) for x in range(size)]

回答by ShadowMitia

Here you can find some more examples on range

在这里你可以找到更多的例子 range

But what you probably want is something like this: for i in range(size, 0):.

但你可能想要的是这样的:for i in range(size, 0):.

回答by musically_ut

For such a construct, where you need to vary the loop index, a forloop might not be the best option.

对于这样的构造,您需要改变循环索引,for循环可能不是最佳选择。

for <var> in <iterable>merely runs the loop body with the <var>taking each value available from the iterable. Changing the <var>will have no effect on the ensuing iterations.

for <var> in <iterable>只是运行循环体,并<var>iterable. 更改<var>将不会影响随后的迭代。

However, since it is usually tricky to work out the sequence of values iwill take beforehand, you can use a whileloop.

但是,由于i事先计算出值的序列通常很棘手,因此您可以使用while循环

i = 0
while i < size:
    if condition:
        ...
        i -= 1
    else:
        ...
        i += 1

回答by Shaik Mahammad Ali

for decrement operator in for loop is

for循环中的递减运算符是

for i in range(5,1,-1): print(i)

对于 i in range(5,1,-1): 打印(i)

it will deceremet by one prints 5432

它会减少一个打印 5432

     for increment index last parameter should be in positive
     for decrement index last parameter should be in negative