Changing the value of a for loop index on the fly

Ron radam2 at tampabay.rr.com
Wed Mar 23 22:57:41 EST 2005


On Thu, 24 Mar 2005 03:42:04 GMT, Ron <radam2 at tampabay.rr.com> 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] = []
>>
>>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.
>>
>
>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[i]'.  
>
>Try this which uses no indexes, it should be closer to what you want.
>

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 = []





More information about the Python-list mailing list