473,608 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ 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_parse r(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 7090
i = 0
while i < len(subject):
if subject[i] in preps:
psubject.append (noun_syn_parse r(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_parse r(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_parse r(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_parse r(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_parse r(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_parse r(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_parse r(subject[0:i]))
subject[0:i] = []
Perhaps (untested!):

start = 0
for i, subj in enumerate(subje ct):
if subj in preps:
psubject.append (noun_syn_parse r(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_parse r(subject[0:i]))
subject[0:i] = []

Perhaps (untested!):

start = 0
for i, subj in enumerate(subje ct):
if subj in preps:
psubject.append (noun_syn_parse r(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_parse r(subject[0:i]))
subject[0:i] = []

Perhaps (untested!):

start = 0
for i, subj in enumerate(subje ct):
if subj in preps:
psubject.append (noun_syn_parse r(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_parse r(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(sub j)
if subj in preps:
psubject.append (noun_sys_parse r(subj2))
subj2 = []
Jul 18 '05 #8
Ron
On Thu, 24 Mar 2005 03:42:04 GMT, Ron <ra****@tampaba y.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_parse r(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(su bj)
if subj in preps:
psubject.append (noun_sys_parse r(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_parse r(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
2278
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 these lines (full text is below): if (document.form1.theDay.options.text != "Wednesday) { var days = document.form1.theDay; days.options.text = days.options.text; <snip> var option - new Option("Wednesday", 2);
16
2112
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 would i do this ??
31
4111
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 populating a simple table. I'd like the table to contain 10 entries per page and have the option for the user to scroll through the pages of data without having to go back to refresh the page (I've already pulled all the info I need from the...
4
1855
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. The dropdown just maintains its default value (element 0).
1
4093
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 Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.White) Then Me.Controls.Item(iSeatNumber).BackColor.Equals(Color.CornflowerBlue) 'End If
7
2077
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 how I might modify a structure in an arraylist without having to completely remove it and re-add the structure to the arraylist? I just want to iterate throught the arraylist and change items based on certain conditions. Thanks in advance!
4
1469
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; mango,green,12,$2.5; orange,orange,8,$1.5;
6
1714
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 (index.rtf). Now I'm adapting the script to convert all the text files into individual html files. What I can't figure out is how to trigger a change in the background value for each folder change (not each file), so that text is color-coded by...
1
18664
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 <= 30; $i++) : ?> <tr> <th><?= $i ?></th>
0
8002
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8496
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8475
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3962
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2474
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 we have to send another system

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.