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

simple question on persisting variable values in a list

P: n/a
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,,,
Nov 20 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 20, 3:25 pm, dgris...@gmail.com wrote:
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:

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

>>lst
['blah', 'blah']

</IDLE session>

Mike
Nov 20 '07 #2

P: n/a
On Nov 20, 9:41 pm, kyoso...@gmail.com wrote:
On Nov 20, 3:25 pm, dgris...@gmail.com wrote:
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:

<IDLE session>
>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]

--
Paul Hankin
Nov 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.