Better way to replace/remove characters in a list of strings.

Discussion in 'Python' started by Chris Brat, Sep 4, 2006.

  1. Chris Brat

    Chris Brat Guest

    Hi,

    Is there a better way to replace/remove characters (specifically ' and
    " characters in my case, but it could be anything) in strings in a
    list, than this example to replace 'a' with 'b':



    x = ["abbbb","123a","nnnnas"]

    for i, v in enumerate(x) :
    x = v.replace("a","b")


    This works, but I'd like to know peoples opinions.

    Thanks
    Chris
     
    Chris Brat, Sep 4, 2006
    #1
    1. Advertising

  2. Chris Brat <> wrote:
    > Is there a better way to replace/remove characters (specifically ' and
    > " characters in my case, but it could be anything) in strings in a
    > list, than this example to replace 'a' with 'b':


    x = map(lambda foo: foo.replace('a', 'b'), x)

    cu
    Philipp

    --
    Dr. Philipp Pagel Tel. +49-8161-71 2131
    Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186
    Technical University of Munich
    http://mips.gsf.de/staff/pagel
     
    Philipp Pagel, Sep 4, 2006
    #2
    1. Advertising

  3. Philipp Pagel wrote:

    > Chris Brat <> wrote:
    > > Is there a better way to replace/remove characters (specifically ' and
    > > " characters in my case, but it could be anything) in strings in a
    > > list, than this example to replace 'a' with 'b':

    >
    > x = map(lambda foo: foo.replace('a', 'b'), x)


    Or more pythonically:

    x = [s.replace('a', 'b') for s in x]

    George
     
    George Sakkis, Sep 4, 2006
    #3
  4. Chris Brat

    Chris Brat Guest

    Thanks, thats exactly what I was looking for - very neat.


    George Sakkis wrote:
    > Philipp Pagel wrote:
    >
    > > Chris Brat <> wrote:
    > > > Is there a better way to replace/remove characters (specifically ' and
    > > > " characters in my case, but it could be anything) in strings in a
    > > > list, than this example to replace 'a' with 'b':

    > >
    > > x = map(lambda foo: foo.replace('a', 'b'), x)

    >
    > Or more pythonically:
    >
    > x = [s.replace('a', 'b') for s in x]
    >
    > George
     
    Chris Brat, Sep 4, 2006
    #4
  5. Chris Brat a écrit :
    > Thanks, thats exactly what I was looking for - very neat.
    >

    Just note that both solutions rebind the name to a newly constructed
    list instead of modifying the original list in place. This is usually
    the RightThing(tm), but sometimes one wants an in-place modification.
     
    Bruno Desthuilliers, Sep 5, 2006
    #5
  6. Chris Brat

    Chris Brat Guest

    Hi

    Wouldn't this only cause problems with large lists - for once off
    scripts with small lists it doesn't seem like a big issue to me.

    Regards,
    Chris

    Bruno Desthuilliers wrote:
    > Chris Brat a écrit :
    > > Thanks, thats exactly what I was looking for - very neat.
    > >

    > Just note that both solutions rebind the name to a newly constructed
    > list instead of modifying the original list in place. This is usually
    > the RightThing(tm), but sometimes one wants an in-place modification.
     
    Chris Brat, Sep 5, 2006
    #6
  7. Chris Brat wrote:

    > Hi
    >
    > Wouldn't this only cause problems with large lists - for once off
    > scripts with small lists it doesn't seem like a big issue to me.
    >
    > Regards,
    > Chris
    >
    > Bruno Desthuilliers wrote:
    > > Chris Brat a écrit :
    > > > Thanks, thats exactly what I was looking for - very neat.
    > > >

    > > Just note that both solutions rebind the name to a newly constructed
    > > list instead of modifying the original list in place. This is usually
    > > the RightThing(tm), but sometimes one wants an in-place modification.



    The extra memory to allocate the new list is usually a minor issue; the
    important one is correctness, if the original list is referenced by
    more than one names. Check the following almost identical-looking
    cases:
    1)
    >>> x = ["abbbb","123a","nnnnas"]
    >>> y = x
    >>> x = [s.replace('a', 'b') for s in x] # rebind to new list
    >>> y is x

    False

    2)
    >>> x = ["abbbb","123a","nnnnas"]
    >>> y = x
    >>> x[:] = [s.replace('a', 'b') for s in x] # in place modification
    >>> y is x

    True

    Neither case is always "the correct"; correctness depends on the
    problem at hand, so you should know the difference and decide between
    rebinding and mutation accordingly.

    George
     
    George Sakkis, Sep 6, 2006
    #7
  8. In <>, George Sakkis
    wrote:

    > Chris Brat wrote:
    >
    >> Wouldn't this only cause problems with large lists - for once off
    >> scripts with small lists it doesn't seem like a big issue to me.

    >
    > The extra memory to allocate the new list is usually a minor issue; the
    > important one is correctness, if the original list is referenced by
    > more than one names.


    It's not the allocation of the new list itself that might be an issue but
    the content, which will be copied in this case, before the old list and
    its content is freed. If you have 200 MiB worth of strings in the list
    and change all 'u's to 'x's with

    large_list = [item.replace('u', 'x') for item in large_list]

    another list with 200 MiB strings will be created.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Sep 6, 2006
    #8
    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. Klaus Neuner
    Replies:
    7
    Views:
    527
    Klaus Neuner
    Jul 26, 2004
  2. rvino
    Replies:
    0
    Views:
    4,720
    rvino
    Aug 14, 2007
  3. anonym
    Replies:
    1
    Views:
    1,073
    Knute Johnson
    Jan 15, 2009
  4. J. Muenchbourg
    Replies:
    4
    Views:
    182
    Andrew Durstewitz
    Jul 11, 2003
  5. Replies:
    12
    Views:
    271
Loading...

Share This Page