Iterate again inside the for loop

Is there a way to repeat the iteration in a for loop? For instance:

for x in list:
  if(condition):
      #I'd like to grab the next iteration of the list 

So, if I had [1,2,3,4], I would iterate through 1 first, and then try to push iteration to 2 in the for loop, so when the loop starts again, it will be 3.

Possible?

I create a parser that reads the if statement, then wants to read the lines until it hits the line that completes the if statement.

0
source share
7 answers

You can do something like this:

a = [1,2,3,4,5]
b = iter(a)

try:
    while True:
        c = b.next()
        if (condition):
            c = b.next()
except StopIteration:
    pass
+3
source

, , , item.next() . StopIteration, .

>>> it = iter(range(5))
>>> for x in it:
...     print x
...     if x > 3:
...         print it.next()
... 
0 1 2 3 4
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
StopIteration
+3

.

for x in list:
  if(condition):
      continue
+1
skip = False
for x in list:
    if skip:
        skip = False
        continue
    # Do your main loop logic here
    if (condition):
        skip = True
+1

while for. :

idx = 0
while idx < length(list)
    x = list[idx]
    ...
    if (condition)
        idx += 1
    ...
    idx += 1
+1

for :

iter = list.__iter__()
while True:
    x = iter.next()
    ...
    if (condition):
        x = iter.next()
    ...

StopIteration, .

+1

, if . , IF- . .

a) BNF. , if

ifstmnt ::=  IF &lt;condiftion> THEN &lt;trueblock> [ ELSE &lt;elseblock> ]

statment ::= ifstmnt | assignstment | whilestmnt | forstmnt 

..

b) , . IF , THEN.

c) getNextToken, , . - - IF, THEN, A number, . , , . , , - . .

d) , . IF, , , , .. , . , , AND, OR .., THEN. THEN , , IF recoogniser , THEN, ( BEGIN, END).

e) - , trueblock, falseblock . - . .

f) , , ,

g) - , - . , , - . , . .

Check out YACC and LEX. These are tools designed to complete individual parts of a task and save you time. Terms that will help your research are “lexical analysis”, “parser” and the like.

And good luck. Your project is non-trivial!

See also http://nedbatchelder.com/text/python-parsers.html

0
source

Source: https://habr.com/ru/post/1791797/


All Articles