Changing the value of a for loop index on the fly

Discussion in 'Python' started by Lonnie Princehouse, Mar 23, 2005.

  1. i = 0
    while i < len(subject):
    if subject in preps:
    psubject.append(noun_syn_parser(subject[0:i]))
    subject[0:i] = []
    i = 0
    else:
    i += 1
     
    Lonnie Princehouse, Mar 23, 2005
    #1
    1. Advertisements

  2. Lonnie Princehouse

    Mitja Guest



    Messing with the list/index concerned within the loop itself is rarely a
    good idea and can most often be avoided:

    lastFound = 0
    for i in range(0, len(subject)):
    if subject in preps:
    psubject.append(noun_syn_parser(subject[lastFound:i]))
    lastFound = i #you probably want i+1 here? your source says i,
    however

    #if having the subject list matters, you can finish it all outside the
    loop with
    subject[0:lastFound] = []


    HTH
    Mitja
     
    Mitja, Mar 23, 2005
    #2
    1. Advertisements

  3. Hi! There goes a newbie trouble:

    for i in range(0, len(subject)):
    if subject in preps:
    psubject.append(noun_syn_parser(subject[0:i]))
    subject[0:i] = []

    Since the last line eliminates some elements of the list, I'm wondering
    if it's somehow possible to change the value of "i" to 0 in order not to
    get an index error. Any other ideas?
    Thanks in advance.
     
    Gabriel F. Alcober, Mar 23, 2005
    #3
  4. Uh! I've not even thought in using a while... Thanks!

     
    Gabriel F. Alcober, Mar 23, 2005
    #4

  5. Perhaps (untested!):

    start = 0
    for i, subj in enumerate(subject):
    if subj in preps:
    psubject.append(noun_syn_parser(subject[start:i]))
    start = i
    subject = subject[start:] # optional if you don't need the leftovers

    Regards,
    Bengt Richter
     
    Bengt Richter, Mar 24, 2005
    #5

  6. Mitja's last line (I didn't see his post -- forwarding delays I presume)
    subject[0:lastFound] = []
    would not rebind, so that better reflects the OP's subject[0:i] = [] line.

    Perhaps the enumerate was useful ;-/

    Regards,
    Bengt Richter
     
    Bengt Richter, Mar 24, 2005
    #6
  7. Lonnie Princehouse

    Ron Guest



    I going to guess that you probably want something more like below.
    The routine you have because of the deletions, the index and the
    subjects list get way out of sync, which is why you get the index
    error on 'subject'.

    Try this which uses no indexes, it should be closer to what you want.

    for subj in subjects:
    sub2.append(subj)
    if subj in preps:
    psubject.append(noun_sys_parser(subj2))
    subj2 = []
     
    Ron, Mar 24, 2005
    #7
  8. Lonnie Princehouse

    Ron Guest



    Correcting a type and an omission:

    subj2 = []
    for subj in subjects:
    subj2.append(subj)
    if subj in preps:
    psubject.append(noun_sys_parser(subj2))
    subj2 = []
     
    Ron, Mar 24, 2005
    #8

  9. Hi! I almost got it but your code is much more simple (and works). I
    hadn't thought in using enumerate.
    You see I'm a completely newbie.
    Thanks a lot, Bengt!
     
    Gabriel F. Alcober, Mar 24, 2005
    #9
  10. Lonnie Princehouse

    Tim Roberts Guest



    You got a lot of good responses to this, but it is a fault of mine that I
    always want people to understand WHY their proposals won't work.

    You CAN, in fact, change "i" within the loop, and your changed value will
    survive until the next iteration. So, this prints 5 copies of 17:

    for i in range(5):
    i = 17
    print i

    However, a "for" loop in Python is quite different from a "for" loop in C
    or Basic. In those languages, the end of a loop is determined by some
    Boolean comparison, so changing the index alters the comparison.

    In the case of Python, the "for" statement just iterates through a set of
    values. "range(5)" is just a normal function that creates the list
    [0,1,2,3,4]. The "for" statement keeps running the loop until it runs out
    of values in that list or tuple.

    So, you CAN change the value of i, but it won't change the operation of the
    loop.
     
    Tim Roberts, Mar 24, 2005
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.