473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Need a bit of help with a list..

Hi all,
Why doesn't this work as expected.. I expect that the the lines within
the sections will get modified - permanently. It is modifying them
during the first section but they never get saved to the new values..
Can anyone help me and even better explain why it's not working..

for section in self.sections:
nidx = 0
for line in self.sections[section]:
if re.match(r"^.*\+$",line):
line = line[:-1]
line = line + " " + self.sections[section][nidx+1]
print nidx, "+ found -total lines",
len(self.sections[section]), line
del self.sections[section][nidx+1]
nidx += 1
else:
nidx += 1

for secs in self.sections:
print " %s" % secs
for line in self.sections[secs]:
print " %s" % line

self.section[foo]=["Param_Set primitive:rdv_AIO_reg50_top_IPTOP
id:chip_top_pads!reg50ma +",
" VDD18:VDD1_CR18 VDD33:VDD1_CR33 VSSA:VSSCORE", "Param_Set
instance:chip_top_pads!PAD_3 APAD:PIN_VTRIP_IN + ", " RC:RC_5A
VDDCR:VDD1_CR18 VDDIO:VDD1_IO33 VSSCR:VSSCORE +"," VDDIO5:VDD1_IO5A
VDDIO5DIV2:VDD1_IO5DIV2A VSSIO:VSS_IO"]

Many thanks!!

Feb 12 '06 #1
2 1158
rh0dium <st**********@gmail.com> wrote:
...
Why doesn't this work as expected.. I expect that the the lines within
the sections will get modified - permanently. It is modifying them
during the first section but they never get saved to the new values..
Can anyone help me and even better explain why it's not working..

for section in self.sections:
nidx = 0
for line in self.sections[section]:
From this constuct I assume self.sections is a dict, in which case there
may be better way to loop over the values in the dict; but that's an
aside and does not affect your stated problem. Rather, said problem is
already shown in the next couple lines:
if re.match(r"^.*\+$",line):
line = line[:-1]
A simple assignment _to a bare name_ (here, 'line') only ever affects
that NAME itself - nothing else, and in particular not the object to
which the name used to be bound before you re-bound it, not other names
(or locations within a container) bound to the same object, and so on.

To affect some item, say the i-th one, of self.sections[section], you
will need to assign something to self.sections[section][i]. You may
give another and nicer name to the whole objects self.sections[section],
but you will still need to assign to whatevername[i] to rebind the i-th
item -- assign to an indexing, not to a bare name.

There's another problem later in this inner loop:
del self.sections[section][nidx+1]


....don't alter the container you're directly looping on, for example by
deleting some of its items: that will alter the semantics of the loop in
way you most definitely don't want. I don't think it's biting you here,
but in most cases it will indeed bite, and painfully.

I would suggest restructuring your whole first nested loop, correcting
other strangeness (which is innocuous) as we go, such as the strange re
and the separate and identical increments of nidx along an if and an
else branch. For example, trying to stay as close as feasible to your
original code, we might have:

for lines in self.sections.itervalues():
nidx = 0
while nidx<len(lines):
line = lines[nidx]
if line.endswith('+'):
lines[nidx] = line[:-1] + " " + lines[nidx+1]
del lines[nidx+1]
nidx += 1

This still has several problems (crashes if there's a + at the end of
the last line, doesn't join properly if two successive lines both end
with +, possibly others since my code is NOT tested) but they're not
horribly hard to fix if they're indeed problems for you.
Alex
Feb 12 '06 #2

Alex Martelli wrote:
From this constuct I assume self.sections is a dict, in which case there
may be better way to loop over the values in the dict; but that's an
aside and does not affect your stated problem. Rather, said problem is
already shown in the next couple lines:
if re.match(r"^.*\+$",line):
line = line[:-1]
A simple assignment _to a bare name_ (here, 'line') only ever affects
that NAME itself - nothing else, and in particular not the object to
which the name used to be bound before you re-bound it, not other names
(or locations within a container) bound to the same object, and so on.


That's what I was thinking it was doing and a simple proof showed me
this..
To affect some item, say the i-th one, of self.sections[section], you
will need to assign something to self.sections[section][i]. You may
give another and nicer name to the whole objects self.sections[section],
but you will still need to assign to whatevername[i] to rebind the i-th
item -- assign to an indexing, not to a bare name.

There's another problem later in this inner loop:
del self.sections[section][nidx+1]
...don't alter the container you're directly looping on, for example by
deleting some of its items: that will alter the semantics of the loop in
way you most definitely don't want. I don't think it's biting you here,
but in most cases it will indeed bite, and painfully.


Thanks so much for pointing this out - it explains some behaviour I was
seeing!!
I would suggest restructuring your whole first nested loop, correcting
other strangeness (which is innocuous) as we go, such as the strange re
and the separate and identical increments of nidx along an if and an
else branch. For example, trying to stay as close as feasible to your
original code, we might have:

for lines in self.sections.itervalues():
nidx = 0
while nidx<len(lines):
line = lines[nidx]
if line.endswith('+'):
lines[nidx] = line[:-1] + " " + lines[nidx+1]
del lines[nidx+1]
nidx += 1


Ah - A couple new methods - Thanks so much for your insightful comments
and concerns. I will take it from here - much appreciated!!

Feb 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
11
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
0
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes...
13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
1
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.