简体   繁体   English

在循环内递增 for 循环

[英]Incrementing a for loop, inside the loop

Is it possible to increment a for loop inside of the loop in python 3?是否可以在python 3的循环内增加for循环?

for example:例如:

for i in range(0, len(foo_list)):
    if foo_list[i] < bar
        i += 4

Where the loop counter i gets incremented by 4 if the condition holds true, else it will just increment by one (or whatever the step value is for the for loop)?如果条件成立,循环计数器i将增加 4,否则它只会增加 1(或 for 循环的任何步长值)?

I know a while loop would be more applicable for an application like this, but it would be good to know if this (or something like this) in a for loop is possible.我知道 while 循环更适用于这样的应用程序,但最好知道 for 循环中的这个(或类似的东西)是否可行。

Thanks!谢谢!

You could use a while loop and increment i based on the condition:您可以使用 while 循环并根据条件递增i

while i < (len(foo_list)): 
    if foo_list[i] < bar: # if condition is True increment by 4
        i += 4
    else: 
        i += 1 # else just increment 1 by one and check next `foo_list[i]`

Using a for loop i will always return to the next value in the range:使用 for 循环, i将始终返回范围内的下一个值:

foo_list = [1,2,3,4,5,6]
bar = 6
for i in range(len(foo_list)):
    print("range i ",i)
    if foo_list[i] < bar:
        i += 4
        print("if i",i)


('range i ', 0)
('if i', 4)
('range i ', 1)
('if i', 5)
('range i ', 2)
('if i', 6)
('range i ', 3)
('if i', 7)
('range i ', 4)
('if i', 8)
('range i ', 5)

In your example as written i will be reset at each new iteration of the loop (which may seem a little counterintuitive), as seen here:在您编写的示例中, i将在循环的每次新迭代中重置(这可能看起来有点违反直觉),如下所示:

foo_list = [1, 2, 3]

for i in range(len(foo_list)):
    print('Before increment:', i)
    i += 4
    print('After increment', i)

>>>
Before increment: 0
After increment 4
Before increment: 1
After increment 5
Before increment: 2
After increment 6

continue is the standard/safe way to skip to the next single iteration of a loop, but it would be far more awkward to chain continues together than to just use a while loop as others suggested. continue是跳到循环的下一个次迭代的标准/安全方式,但是将continues在一起比像其他人建议的那样只使用while循环要尴尬得多。

a bit hackish...有点黑客...

>>> b = iter(range(10))
>>> for i in b:
...     print(i)
...     if i==5 : i = next(b)
... 
0
1
2
3
4
5
7
8
9
>>> 
while i < end:
  # do stuff
  # maybe i +=1
  # or maybe i += 4

I suppose you could do this in a for loop if you tried, but it's not advisable.如果您尝试过,我想您可以在 for 循环中执行此操作,但不建议这样做。 The whole point of python's for loop is to look at items, not indices python for 循环的重点是查看项目,而不是索引

It is possible to achieve this with a for loop using the send method of a generator, where the counter can be modified if the generator is sent a new value:可以通过使用生成器的send方法的for循环来实现这一点,如果向生成器发送新值,则可以修改计数器:

def jumpable_range(end):
    counter = 0
    while counter < end:
        jump = yield counter
        if jump is None:
            counter += 1
        else:
            counter = jump

foo_list = [1, 2, 3, 4, 5, 6, 7, 8]
bar = 6
iterable = jumpable_range(len(foo_list))
for i in iterable:
    if foo_list[i] < bar:
        i = iterable.send(i + 4)
    print(i)

This outputs:这输出:

4
5
6
7

Demo: https://repl.it/@blhsing/ImpossibleAcrobaticComputergraphics演示: https : //repl.it/@blhsing/ImpossibleAcrobaticComputergraphics

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

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