simple question on persisting variable values in a list

Discussion in 'Python' started by dgrissen@gmail.com, Nov 20, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Guest

    On Nov 20, 3:25 pm, 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, 2007
    #2
    1. Advertising

  3. Paul Hankin Guest

    On Nov 20, 9:41 pm, wrote:
    > On Nov 20, 3:25 pm, 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
    Paul Hankin, Nov 20, 2007
    #3
  4. On Tue, 20 Nov 2007 13:25:05 -0800 (PST), declaimed
    the following in comp.lang.python:

    > I am an unabashed noob.
    >

    <no comment>

    > 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
    Dennis Lee Bieber, Nov 21, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    Persisting values on a webform

    John, Oct 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    384
    Xavier Pacheco
    Oct 10, 2004
  2. Server Control
    Replies:
    4
    Views:
    758
  3. Carlos
    Replies:
    3
    Views:
    5,017
    PeterKellner
    May 17, 2006
  4. Falcolas
    Replies:
    3
    Views:
    236
    Bruno Desthuilliers
    Nov 5, 2007
  5. *no spam*

    Values not persisting in extender provider

    *no spam*, Oct 28, 2004, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    181
Loading...

Share This Page