473,395 Members | 1,379 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,395 software developers and data experts.

simple question on persisting variable values in a list

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

Similar topics

5
by: James Baker | last post by:
I have a form that has a dropdown list that will cause a post to the same page when it's changed. The problem I'm running into is that all of the controls reset to their default values (obviously...
4
by: jlea | last post by:
Is there an option in the schema definition to indicate that when xsd.exe generates classes for the schema that a certain element will not get serialized to and from XML? In other words, I would...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
1
by: lim | last post by:
What is the possible error that occurs when the Page_load event is not triggered during execution. In my page there's some basic server control. Is there any loops holes?
2
by: Chris Ashley | last post by:
My app seems to lose its viewstate completely when I modify the querystring. Anything I can do about this? Cheers, Chris
4
by: Server Control | last post by:
Hi, I have a datagrid(within a user control) that has label controls. I have added hidden fields in the datagrid alongwitht the label control. I do some client side editing with javascript and...
12
by: jimfortune | last post by:
I have a question based somewhat on: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/ddde992b84f762bd/152bbc027bf00720?hl=en#152bbc027bf00720 A local table works well...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
1
by: David Veeneman | last post by:
I'm backporting a component to .NET 1.x, and it required me to use a custom collection, StringList, instead of List<string>. StringList is derived from CollectionBase and is marked serializable. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...

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.