473,480 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing the value of a for loop index on the fly

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
9 7081
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
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
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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2252
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
16
2096
by: chris | last post by:
im new to javascript but slowly getting better what i want to do is have some text on the screen and when an event happens for example click a button the text would change to what i want. how...
31
4069
by: Greg Scharlemann | last post by:
Given some recent success on a simple form validation (mainly due to the kind folks in this forum), I've tried to tackle something a bit more difficult. I'm pulling data down from a database and...
4
1847
by: Simon Harvey | last post by:
Hi all, Am I being really stupid here: myDropDown.SelectedIndex = 2 I think this line should set the dropdown control's selected item to 2. But nothing seems to be happening on the page....
1
4085
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If...
7
2067
by: Adam | last post by:
Hi all, In my VB.NET code below, I try to change the user name in my arraylist from Ted to Bob, but instead it adds a new user to the arraylist named Bob. Can anyone explain why this happens and...
4
1461
by: aqazi | last post by:
Hi guys I am having a problem with arrayu manipulation. in my php script i am reading from a csv file. the content of file is like this: name,color,quantity,price; apple,red,10,$2;...
6
1706
by: PipedreamerGrey | last post by:
I'm using the script below (originally from http://effbot.org, given to me here) to open all of the text files in a directory and its subdirectories and combine them into one Rich text file...
1
18614
by: sksksk | last post by:
I want to achieve the following process in the smarty for $item one i should be able to get the value using loop.index, but without any luck. any help is appreciated. <?php for ($i = 1; $i...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.