By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Changing the value of a for loop index on the fly

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
i = 0
while i < len(subject):
if subject[i] 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[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.


Jul 18 '05 #2

P: n/a
Uh! I've not even thought in using a while... Thanks!

Lonnie Princehouse wrote:
i = 0
while i < len(subject):
if subject[i] 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[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.



Jul 18 '05 #3

P: n/a
On Wed, 23 Mar 2005 23:27:54 +0100, Gabriel F. Alcober <gf*****@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?


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[i] 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
Jul 18 '05 #4

P: n/a
On Wed, 23 Mar 2005 23:27:54 +0100, "Gabriel F. Alcober" <gf*****@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

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
Jul 18 '05 #5

P: n/a
On Thu, 24 Mar 2005 02:48:28 GMT, bo**@oz.net (Bengt Richter) wrote:
On Wed, 23 Mar 2005 23:27:54 +0100, "Gabriel F. Alcober" <gf*****@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
Jul 18 '05 #6

P: n/a
Bengt Richter wrote:
On Thu, 24 Mar 2005 02:48:28 GMT, bo**@oz.net (Bengt Richter) wrote:
On Wed, 23 Mar 2005 23:27:54 +0100, "Gabriel F. Alcober" <gf*****@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!
Jul 18 '05 #7

P: n/a
Ron
On Wed, 23 Mar 2005 23:27:54 +0100, "Gabriel F. Alcober"
<gf*****@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.

for subj in subjects:
sub2.append(subj)
if subj in preps:
psubject.append(noun_sys_parser(subj2))
subj2 = []
Jul 18 '05 #8

P: n/a
Ron
On Thu, 24 Mar 2005 03:42:04 GMT, Ron <ra****@tampabay.rr.com> wrote:
On Wed, 23 Mar 2005 23:27:54 +0100, "Gabriel F. Alcober"
<gf*****@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 = []
Jul 18 '05 #9

P: n/a
"Gabriel F. Alcober" <gf*****@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?


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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.