Changing the value of a for loop index on the fly

Gabriel F. Alcober gfa1977 at yahoo.ca
Wed Mar 23 23:30:59 EST 2005


Bengt Richter wrote:

>On Thu, 24 Mar 2005 02:48:28 GMT, bokr at oz.net (Bengt Richter) wrote:
>
>  
>
>>On Wed, 23 Mar 2005 23:27:54 +0100, "Gabriel F. Alcober" <gfa1977 at yahoo.ca> wrote:
>>
>>    
>>
>>>Hi! There goes a newbie trouble:
>>>
>>>for i in range(0, len(subject)):
>>>       if subject[i] in preps:
>>>           psubject.append(noun_syn_parser(subject[0:i]))
>>>           subject[0:i] = []
>>>
>>>      
>>>
>>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
>>
>>    
>>
>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
>  
>
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!





More information about the Python-list mailing list