Iterate again within the for loop

前端 未结 7 1011
走了就别回头了
走了就别回头了 2021-01-21 07:50

is there a way to iterate again within the for loop? For example:

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


        
相关标签:
7条回答
  • 2021-01-21 08:27

    You can always bypass the for loop and just use the iterator explicitly:

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

    This will throw a StopIteration exception when it reaches the end of the list.

    0 讨论(0)
  • 2021-01-21 08:28
    skip = False
    for x in list:
        if skip:
            skip = False
            continue
        # Do your main loop logic here
        if (condition):
            skip = True
    
    0 讨论(0)
  • 2021-01-21 08:30

    You could 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
    
    0 讨论(0)
  • 2021-01-21 08:42

    You want the continue statement.

    for x in list:
      if(condition):
          continue
    
    0 讨论(0)
  • 2021-01-21 08:43

    If the item you're using over is an iterable object you can use item.next() to grab the next element. But you'll need to make sure to grab the StopIteration exception if needed.

    >>> 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
    
    0 讨论(0)
  • 2021-01-21 08:43

    I suspect that your approach to parsing an if statement is not a good way to go about it. You will also find that you can't nest your IF statements nor put them all one one line. Here is how I would go about it.

    a) Convert your language into a BNF form. For example, for an if statment it might be

    ifstmnt ::=  IF &lt;condiftion> THEN &lt;trueblock> [ ELSE &lt;elseblock> ]
    
    statment ::= ifstmnt | assignstment | whilestmnt | forstmnt 
    

    etc

    b) Work out what you are waiting for at each point. After the IF, you read a condition until you see THEN.

    c) Write a getNextToken routine that reads characters from your source, until it has a complete token. A token is a recognisable unit - IF, THEN, A number, a symbol. Every time it is called it returns the token into a buffer. It is usefull to have a type and a value returned as well - it will save you converting numbers in many places. A table driven approach is very fast and flexible.

    d) Then write lots of little routines to recognise each type of statement. One for IF, one for condition, one for block, one for statement, one for expression etc. These will call each other and return when they have recognised the statement. For example, the condition expression recogniser will read a boolean expression and eat all names, AND, OR etc, until it looks ahead and sees THEN. It can't handle THEN so it exists, and the IF recoogniser finds the token is THEN, it reads the next and calls the recogniser for a block (which might expect BEGIN, lots of statments and then END).

    e) Each routine collects the data it needs - condition, trueblock, falseblock, and handles it as required. A very common treatment is to create a tree representation of the program in memory. Programmer defined names are collected in a dictionary as they are defined, and checked as they are used.

    f) A real compiler will then try to re-arange the tree to make things more efficent - but I suggest that is a future develoment

    g) The final action is then to walk the tree, evaluating it in some way. If you are writng an interpreter you can calulate the values and store then as you go - store the values in the doctionary. If you are writting a true compiler you will need to generate a suitable output for the linker. That is a major task.

    Check out YACC and LEX. They are tools designed to do parts of the job, and will save you time. The terms to help your research are "lexical analysis" "parser" and similar.

    And good luck. Your project is non-trivial!

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

    0 讨论(0)
提交回复
热议问题