Replace Several Items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gjhames

    #1

    Replace Several Items

    I wish to replace several characters in my string to only one.
    Example, "-", "." and "/" to nothing ""
    I did like that:
    my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
    "").replace(")" , "").replace("(" , "")

    But I think it's a ugly way.

    What's the better way to do it?
  • bearophileHUGS@lycos.com

    #2
    Re: Replace Several Items

    gjhames:
    What's the better way to do it?
    Better is a relative term. If with better you mean "faster" (in some
    circumstances), then the translate method is your friend, as you can
    see its second argument are the chars to be removed. As first argument
    you can use something like:
    "".join(map(chr , xrange(256)))
    If your strings are unicode you will need something different (a dict
    with Null values for the key chars you want to remove).

    Bye,
    bearophile

    Comment

    • Eric Wertman

      #3
      Re: Replace Several Items

      I tend to use the re module like so :

      import re
      my_string = re.sub('[\-,./]','',my_string)
      I wish to replace several characters in my string to only one.
      Example, "-", "." and "/" to nothing ""
      I did like that:
      my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
      "").replace(")" , "").replace("(" , "")
      >
      But I think it's a ugly way.
      >
      What's the better way to do it?

      Comment

      • Wojtek Walczak

        #4
        Re: Replace Several Items

        Dnia Wed, 13 Aug 2008 09:39:53 -0700 (PDT), gjhames napisa³(a):
        I wish to replace several characters in my string to only one.
        Example, "-", "." and "/" to nothing ""
        I did like that:
        my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
        "").replace(")" , "").replace("(" , "")
        >
        But I think it's a ugly way.
        >
        What's the better way to do it?
        The regular expression is probably the best way to do it,
        but if you really want to use replace, you can also use
        the replace method in loop:
        >>somestr = "Qwe.Asd/Zxc()Poi-Lkj"
        >>for i in '-./()':
        .... somestr = somestr.replace (i, '')
        ....
        >>somestr
        'QweAsdZxcPoiLk j'
        >>>

        Next step would be to define your own replacing function:

        def my_replace(myst r, mychars, myrepl):
        """Replace every character from 'mychars' string with 'myrepl' string
        in 'mystr' string.

        Example:

        my_replace('Qwe .Asd/Zxc(', './(', 'XY') -'QweXYAsdXYZxcX Y'"""

        for i in mychars:
        mystr = mystr.replace(i , myrepl)

        return mystr


        --
        Regards,
        Wojtek Walczak,

        Comment

        • Fredrik Lundh

          #5
          Re: Replace Several Items

          Wojtek Walczak wrote:
          >I wish to replace several characters in my string to only one.
          >Example, "-", "." and "/" to nothing ""
          >I did like that:
          >my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
          >"").replace(") ", "").replace("(" , "")
          >>
          >But I think it's a ugly way.
          >>
          >What's the better way to do it?
          >
          The regular expression is probably the best way to do it,
          but if you really want to use replace, you can also use
          the replace method in loop:
          suggested exercise: benchmark re.sub with literal replacement, re.sub
          with callback (lambda m: ""), repeated replace, and repeated use of the form

          if ch in my_string:
          my_string = my_string.repla ce(ch, "")

          on representative data.

          </F>

          Comment

          • bearophileHUGS@lycos.com

            #6
            Re: Replace Several Items

            Fredrik Lundh:
            suggested exercise: benchmark re.sub with literal replacement, re.sub
            with callback (lambda m: ""), repeated replace, and repeated use of the form
            ....
            on representative data.
            Please, add the translate() solution too I have suggested :-)

            Bye,
            bearophile

            Comment

            • John Krukoff

              #7
              Re: Replace Several Items

              On Wed, 2008-08-13 at 09:39 -0700, gjhames wrote:
              I wish to replace several characters in my string to only one.
              Example, "-", "." and "/" to nothing ""
              I did like that:
              my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
              "").replace(")" , "").replace("(" , "")
              >
              But I think it's a ugly way.
              >
              What's the better way to do it?
              --
              http://mail.python.org/mailman/listinfo/python-list

              The maketrans interface is a bit clunky, but this is what
              string.translat e is best at:

              >>import string
              >>'-./other'.translat e( string.maketran s( '', '' ), '-./' )
              'other'

              It'd be interesting to see where it falls in the benchmarks, though.

              It's worth noting that the interface for translate is quite different
              for unicode strings.
              --
              John Krukoff <jkrukoff@ltgc. com>
              Land Title Guarantee Company

              Comment

              • Wojtek Walczak

                #8
                Re: Replace Several Items

                Dnia Wed, 13 Aug 2008 23:31:42 +0200, Fredrik Lundh napisa³(a):
                >>I wish to replace several characters in my string to only one.
                >>Example, "-", "." and "/" to nothing ""
                >>I did like that:
                >>my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
                >>"").replace(" )", "").replace("(" , "")
                >>>
                >>But I think it's a ugly way.
                >>>
                >>What's the better way to do it?
                >>
                >The regular expression is probably the best way to do it,
                >but if you really want to use replace, you can also use
                >the replace method in loop:
                >
                suggested exercise: benchmark re.sub with literal replacement, re.sub
                with callback (lambda m: ""), repeated replace, and repeated use of the form
                >
                if ch in my_string:
                my_string = my_string.repla ce(ch, "")
                >
                on representative data.
                I don't have to, I can anticipate the results. I mentioned above
                that using re is the best approach, but if one really wants to use
                replace() multiple times (which will be slow, of course), it can
                be done a bit cleaner than with str.replace().r eplace().replac e()...

                --
                Regards,
                Wojtek Walczak,

                Comment

                • Fredrik Lundh

                  #9
                  Re: Replace Several Items

                  Wojtek Walczak wrote:
                  >suggested exercise: benchmark re.sub with literal replacement, re.sub
                  >with callback (lambda m: ""), repeated replace, and repeated use of the form
                  >>
                  > if ch in my_string:
                  > my_string = my_string.repla ce(ch, "")
                  >>
                  >on representative data.
                  >
                  I don't have to, I can anticipate the results.
                  Chances are that you're wrong.

                  </F>

                  Comment

                  • Wojtek Walczak

                    #10
                    Re: Replace Several Items

                    Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
                    >> if ch in my_string:
                    >> my_string = my_string.repla ce(ch, "")
                    >>>
                    >>on representative data.
                    >>
                    >I don't have to, I can anticipate the results.
                    >
                    Chances are that you're wrong.
                    At the moment my average is about 0.75 of mistake per
                    post on comp.lang.pytho n (please, bare with me ;-)).
                    I strongly believe that the statement I made above won't
                    make this number rise.

                    :)
                    --
                    Regards,
                    Wojtek Walczak,

                    Comment

                    • John Machin

                      #11
                      Re: Replace Several Items

                      On Aug 14, 8:50 am, Wojtek Walczak
                      <gmin...@nie.ma .takiego.adresu .w.sieci.plwrot e:
                      Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
                      >
                      > if ch in my_string:
                      > my_string = my_string.repla ce(ch, "")
                      >
                      >on representative data.
                      >
                      I don't have to, I can anticipate the results.
                      >
                      Chances are that you're wrong.
                      >
                      At the moment my average is about 0.75 of mistake per
                      post on comp.lang.pytho n (please, bare with me ;-)).
                      I strongly believe that the statement I made above won't
                      make this number rise.
                      Meta-mistake: Playing poker with the effbot. If he says diffidently
                      that he'll raise you a dime, it means he's holding five aces :-)

                      Clue: The effbot was the original author of the modern (Python 1.6?)
                      version of the re module.

                      HTH,
                      John


                      Comment

                      • Steven D'Aprano

                        #12
                        Re: Replace Several Items

                        On Wed, 13 Aug 2008 22:50:29 +0000, Wojtek Walczak wrote:
                        Dnia Thu, 14 Aug 2008 00:31:00 +0200, Fredrik Lundh napisa³(a):
                        >
                        >>> if ch in my_string:
                        >>> my_string = my_string.repla ce(ch, "")
                        >>>>
                        >>>on representative data.
                        >>>
                        >>I don't have to, I can anticipate the results.
                        >>
                        >Chances are that you're wrong.
                        >
                        At the moment my average is about 0.75 of mistake per post on
                        comp.lang.pytho n (please, bare with me ;-)). I strongly believe that the
                        statement I made above won't make this number rise.
                        >
                        :)


                        Okay, is this going to be one of those things where, no matter what the
                        benchmarks show, you say "I was right, I *did* anticipate the results. I
                        just anticipated them correctly/incorrectly."?

                        If so, you get an A+ in pedantry and F- in usefulness *wink*

                        In full knowledge that Python is relatively hard to guess what is fast
                        compared to what is slow, I'll make my guess of fastest to slowest:

                        1. repeated replace
                        2. repeated use of the form
                        "if ch in my_string: my_string = my_string.repla ce(ch, "")
                        3. re.sub with literal replacement
                        4. re.sub with callback (lambda m: "")


                        Results to follow.


                        --
                        Steven

                        Comment

                        • Fredrik Lundh

                          #13
                          Re: Replace Several Items

                          Steven D'Aprano wrote:
                          While I'm gratified that my prediction was so close to the results I
                          found, I welcome any suggestions to better/faster/more efficient code.
                          more things to try:
                          code tweaks:

                          - Factor out the creation of the regular expression from the tests:
                          "escape" and "compile" are relatively expensive, and neither throw-away
                          code (using the RE function forms) nor production code will end up doing
                          them both for each string.

                          - Same w. the translation table for "translate"

                          - Use Unicode strings instead of byte strings (we're moving towards 3.0,
                          after all).

                          test data variations:

                          - Try dropping the number of actual replacements and see what happens --
                          if you're escaping user-provided data (e.g. HTML), for example, it's not
                          that unlikely that you end up doing only a few replacements for each
                          string you're processing, or no replacements at all.

                          - Also try shorter and longer strings ("human-sized" data is often
                          provided in shorter chunks than 216 characters per string; the typical
                          size and distribution depends on your actual application, of course).

                          Unicode will affect translate more than the others; the last two will
                          most likely affect in-replace instead (that approach gets faster the
                          shorter the strings are, and the fewer calls to replace that you
                          actually end up doing).

                          Finally, if you want the sub-lambda form to look better, try inserting a
                          character before or after each special character using a template string
                          or a lambda (e.g. a backslash).

                          </F>

                          Comment

                          • Fredrik Lundh

                            #14
                            Re: Replace Several Items

                            John Machin wrote:
                            Clue: The effbot was the original author of the modern (Python 1.6?)
                            version of the re module.
                            And the author of the "in" and "replace" implementations in Python 2.5.

                            </F>

                            Comment

                            • M.-A. Lemburg

                              #15
                              Re: Replace Several Items

                              On 2008-08-13 23:54, John Krukoff wrote:
                              On Wed, 2008-08-13 at 09:39 -0700, gjhames wrote:
                              >I wish to replace several characters in my string to only one.
                              >Example, "-", "." and "/" to nothing ""
                              >I did like that:
                              >my_string = my_string.repla ce("-", "").replace("." , "").replace ("/",
                              >"").replace(") ", "").replace("(" , "")
                              >>
                              >But I think it's a ugly way.
                              >>
                              >What's the better way to do it?
                              >--
                              >http://mail.python.org/mailman/listinfo/python-list
                              >
                              >
                              The maketrans interface is a bit clunky, but this is what
                              string.translat e is best at:
                              >
                              >>import string
                              >>>'-./other'.translat e( string.maketran s( '', '' ), '-./' )
                              'other'
                              >
                              It'd be interesting to see where it falls in the benchmarks, though.
                              >
                              It's worth noting that the interface for translate is quite different
                              for unicode strings.
                              Right. Unicode .translate() uses a dictionary for defining the
                              mapping.

                              Another approach is to use the re module:
                              >>import re
                              >>re.sub('[-./()]', '', '-./other')
                              'other'

                              --
                              Marc-Andre Lemburg
                              eGenix.com

                              Professional Python Services directly from the Source (#1, Aug 14 2008)
                              >>Python/Zope Consulting and Support ... http://www.egenix.com/
                              >>mxODBC.Zope.D atabase.Adapter ... http://zope.egenix.com/
                              >>mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
                              _______________ _______________ _______________ _______________ ____________

                              :::: Try mxODBC.Zope.DA for Windows,Linux,S olaris,MacOSX for free ! ::::


                              eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
                              D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
                              Registered at Amtsgericht Duesseldorf: HRB 46611

                              Comment

                              Working...