Changing the value of a for loop index on the fly

L

Lonnie Princehouse

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
 
M

Mitja

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?


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
 
G

Gabriel F. Alcober

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.
 
G

Gabriel F. Alcober

Uh! I've not even thought in using a while... Thanks!

Lonnie said:
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

Gabriel F. Alcober wrote:

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.
 
B

Bengt Richter

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

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

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.

Regards,
Bengt Richter
 
B

Bengt Richter

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

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
 
R

Ron

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.


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

Ron

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.


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.


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

Gabriel F. Alcober

Bengt said:
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] = []

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!
 
T

Tim Roberts

Gabriel F. Alcober said:
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?


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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,573
Members
45,046
Latest member
Gavizuho

Latest Threads

Top