简体   繁体   English

Python 用于循环递减索引

[英]Python for loop decrementing index

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 if statement, but apparently I can't do that.如果它在if语句中,我尝试将索引减少 1,但显然我不能这样做。 Is there any other way that I can decrease i in a for loop?有没有其他方法可以在 for 循环中减少i

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范围的文档(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. 'start'参数定义何时应该从你的情况'i'的计数器变量开始。 The 'end' parameter is essentially what the size parameter does in your case. 'end'参数基本上就是size参数在你的情况下的作用。 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. 至于你想要什么,因为你希望每个循环减少变量'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循环计数'i'分别在每个增量2减少或减少4减少。

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...? 编辑:刚刚注意到你可能想要减少for循环中'i'值的事实......? 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. 我会为此做的只是有一个while循环而不是你将有一个暴露的变量,你可以随心所欲地修改。

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

For such a construct, where you need to vary the loop index, a for loop 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 . for <var> in <iterable>只运行循环体, <var>iterable获取每个值。 Changing the <var> will have no effect on the ensuing iterations. 更改<var>将不会影响随后的迭代。

However, since it is usually tricky to work out the sequence of values i will take beforehand, you can use a while loop . 但是,由于计算出i将事先采用的值序列通常很棘手,因此可以使用while循环

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

You should not mutate i . 你不应该改变i The for i in range(size): is kind of special. for i in range(size):for i in range(size):有点特别。 Instead you should probably use a list comprehension: 相反,你应该使用列表理解:

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)]

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):

for decrement operator in for loop is for for循环中的递减运算符是

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

it will deceremet by one prints 5432 它将通过一个印刷品5432进行deceremet

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM