simple question on persisting variable values in a list

D

dgrissen

Hi,

I am an unabashed noob.

I am trying to build a list to store values that are generated through
a loop. However, every time I append the variable to the list, I'd
like to reset the variable, but have the value persist in the loop. I
understand why this doesn't work because it's a reference not a
literal value but have been unsuccessful at using copy.copy() or
anything else to accomplish this:


for char in splitlines[r]:

if char == "\n":
temp = copy.deepcopy(templine)
individline.append(temp)
templine = ""
else:
templine += char

results.append(individline)


This just gives me the last element in splitlines[r] - what i want is
to persist each line that is parsed from splitlines[] into the results
list.

Appreciate any help,,,
 
K

kyosohma

Hi,

I am an unabashed noob.

I am trying to build a list to store values that are generated through
a loop. However, every time I append the variable to the list, I'd
like to reset the variable, but have the value persist in the loop. I
understand why this doesn't work because it's a reference not a
literal value but have been unsuccessful at using copy.copy() or
anything else to accomplish this:

for char in splitlines[r]:

if char == "\n":
temp = copy.deepcopy(templine)
individline.append(temp)
templine = ""
else:
templine += char

results.append(individline)

This just gives me the last element in splitlines[r] - what i want is
to persist each line that is parsed from splitlines[] into the results
list.

Appreciate any help,,,

Not sure if this is what you want, but here's what I did:

x = 'blah\nblah\n'
lst = []
templine = ''
for char in x:
if char == '\n':
temp = templine
lst.append(temp)
templine = ''
else:
templine += char

['blah', 'blah']

</IDLE session>

Mike
 
P

Paul Hankin

I am an unabashed noob.
I am trying to build a list to store values that are generated through
a loop. However, every time I append the variable to the list, I'd
like to reset the variable, but have the value persist in the loop. I
understand why this doesn't work because it's a reference not a
literal value but have been unsuccessful at using copy.copy() or
anything else to accomplish this:
for char in splitlines[r]:
if char == "\n":
temp = copy.deepcopy(templine)
individline.append(temp)
templine = ""
else:
templine += char
results.append(individline)

This just gives me the last element in splitlines[r] - what i want is
to persist each line that is parsed from splitlines[] into the results
list.
Appreciate any help,,,

Not sure if this is what you want, but here's what I did:

x = 'blah\nblah\n'
lst = []
templine = ''
for char in x:

if char == '\n':
temp = templine
lst.append(temp)
templine = ''
else:
templine += char

No need for all that code, because you're reimplementing the 'split'
method of strings:

x = 'blah\nblah\n'
lst = x.split('\n')[:-1]
 
D

Dennis Lee Bieber

I am an unabashed noob.
said:
like to reset the variable, but have the value persist in the loop. I
understand why this doesn't work because it's a reference not a
literal value but have been unsuccessful at using copy.copy() or

It works perfectly well... append() puts a reference to the object
onto the end of a list. The subsequent assignment changes the
/reference/, not the object, to refer to some other object.

Also, strings are immutable -- one can not change a string; one can
only create a new string object, and then change the reference to that
object.
anything else to accomplish this:


for char in splitlines[r]:

What is r ? What is splitlines ?

Right now, all I can interpret is that you are taking the individual
characters of the r'th entry of a list of lines...
if char == "\n":
temp = copy.deepcopy(templine)

Unneeded operation
individline.append(temp)

Where do you initialize individline?
templine = ""
else:
templine += char

The slowest way to build up a string... Each time you do this you
are making a new string that is one character longer, and throwing the
old string away to be garbage collected.
results.append(individline)

Let's see... this appends "individline" to some results list... But
above you append a string to individline list... So this is appending a
list to another list (not extending it, you are nesting lists)...

The only action I can interpret from all this is that you have a
list of lines that are terminated by \n, and want to create a list of
lines that don't have the \n...

results = [ln.strip("\n") for ln in splitlines]

Or, if splitlines isn't a list of lines, but just one long string
with embedded \n...

results = splitlines.split("\n")

In either of these cases, it all turns into nice, fast, one-liner...

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top