replacing substrings within strings

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

    #1

    replacing substrings within strings

    Hi
    I was wondering if there was a nicer way to swap the first 2
    characters in a string with the 4th and 5th characters other than:

    darr=list("0102 03040506")
    aarr=darr[:2]
    barr=darr[4:6]
    darr[:2]=barr
    darr[4:6]=aarr
    result="".join( darr)

    The above code works fine but I was wondering if anybody had another
    way of doing this?

    A

  • Diez B. Roggisch

    #2
    Re: replacing substrings within strings

    amadain wrote:
    Hi
    I was wondering if there was a nicer way to swap the first 2
    characters in a string with the 4th and 5th characters other than:
    >
    darr=list("0102 03040506")
    aarr=darr[:2]
    barr=darr[4:6]
    darr[:2]=barr
    darr[4:6]=aarr
    result="".join( darr)
    >
    The above code works fine but I was wondering if anybody had another
    way of doing this?

    You can do it like this:


    darr=list("0102 03040506")
    darr[:2], darr[4:5] = darr[4:6], darr[:2]
    result="".join( darr)
    print result


    Diez

    Comment

    • Rune Strand

      #3
      Re: replacing substrings within strings

      On Feb 14, 1:08 pm, "amadain" <mfmdev...@gmai l.comwrote:
      I was wondering if there was a nicer way to swap the first 2
      characters in a string with the 4th and 5th characters other than:
      >
      darr=list("0102 03040506")
      aarr=darr[:2]
      barr=darr[4:6]
      darr[:2]=barr
      darr[4:6]=aarr
      result="".join( darr)
      >
      The above code works fine but I was wondering if anybody had another
      way of doing this?
      Assuming the string length is always at least 5:

      def swap(s):
      return '%s%s%s%s' % (s[3:6], s[2:3], s[:2], s[6:])


      Comment

      • amadain

        #4
        Re: replacing substrings within strings

        On Feb 14, 12:16 pm, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
        amadain wrote:
        Hi
        I was wondering if there was a nicer way to swap the first 2
        characters in a string with the 4th and 5th characters other than:
        >
        darr=list("0102 03040506")
        aarr=darr[:2]
        barr=darr[4:6]
        darr[:2]=barr
        darr[4:6]=aarr
        result="".join( darr)
        >
        The above code works fine but I was wondering if anybody had another
        way of doing this?
        >
        You can do it like this:
        >
        darr=list("0102 03040506")
        darr[:2], darr[4:5] = darr[4:6], darr[:2]
        result="".join( darr)
        print result
        >
        Diez


        Thats the same code. I was wondering if the string manipulation can be
        done without an excursion into the list world.

        A

        Comment

        • Maric Michaud

          #5
          Re: replacing substrings within strings

          Le mercredi 14 février 2007 13:08, amadain a écrit :
          darr=list("0102 03040506")
          aarr=darr[:2]
          barr=darr[4:6]
          darr[:2]=barr
          darr[4:6]=aarr
          result="".join( darr)
          >
          The above code works fine but I was wondering if anybody had another
          way of doing this?
          Why not :

          In [4]: s="010203040506 "

          In [5]: print s[4:6] + s[2:4] + s[0:2] + s[6:]
          030201040506

          ?

          --
          _____________

          Maric Michaud
          _____________

          Aristote - www.aristote.info
          3 place des tapis
          69004 Lyon
          Tel: +33 426 880 097
          Mobile: +33 632 77 00 21

          Comment

          • Sion Arrowsmith

            #6
            Re: replacing substrings within strings

            amadain <mfmdevine@gmai l.comwrote:
            >I was wondering if there was a nicer way to swap the first 2
            >characters in a string with the 4th and 5th characters other than:
            >
            >darr=list("010 203040506")
            >aarr=darr[:2]
            >barr=darr[4:6]
            >darr[:2]=barr
            >darr[4:6]=aarr
            >result="".join (darr)
            darr=list("0102 03040506")
            darr[:2], darr[4:6] = darr[4:6], darr[:2]
            result = "".join(dar r)

            --
            \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
            ___ | "Frankly I have no feelings towards penguins one way or the other"
            \X/ | -- Arthur C. Clarke
            her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

            Comment

            • Rune Strand

              #7
              Re: replacing substrings within strings

              Or, slighly slower, but more general:

              def swap(s, order=(3,4,2,0, 1)):
              # assert len(s) >= len(order)
              return ''.join([s[o] for o in order]) + s[6:]




              Comment

              • Diez B. Roggisch

                #8
                Re: replacing substrings within strings

                Thats the same code. I was wondering if the string manipulation can be
                done without an excursion into the list world.
                That's the price to pay for immutable strings. If you have to to lots of
                stuff like that, then keep things a list, and join only when you need the
                result as a string.

                Diez

                Comment

                • bearophileHUGS@lycos.com

                  #9
                  Re: replacing substrings within strings

                  Diez B. Roggisch:
                  That's the price to pay for immutable strings.
                  Right, but CPython has array.array("c" ) too. Using Diez Roggisch's
                  code:
                  >>from array import array
                  >>arrs = array("c", "0102030405 06")
                  >>arrs[:2], arrs[4:5] = arrs[4:6], arrs[:2]
                  >>arrs.tostring ()
                  '0302013040506'

                  Using such arrays is useful if you have to do lot of processing before
                  the final tostring().

                  Bye,
                  bearophile

                  Comment

                  • amadain

                    #10
                    Re: replacing substrings within strings

                    Thanks all. I usually turn strings into arrays for processing. I was
                    looking to see if that was the best way to do it from others that use
                    python. No one else uses python in my company so its nice to get
                    different points of view from other python users from lists like this.
                    A

                    Comment

                    • Diez B. Roggisch

                      #11
                      Re: replacing substrings within strings

                      bearophileHUGS@ lycos.com wrote:
                      Diez B. Roggisch:
                      >That's the price to pay for immutable strings.
                      >
                      Right, but CPython has array.array("c" ) too. Using Diez Roggisch's
                      code:
                      Ahhh, ze arrayz. I alwayz forget about the arrayz.

                      Diez

                      Comment

                      • Paul McGuire

                        #12
                        Re: replacing substrings within strings

                        On Feb 14, 6:08 am, "amadain" <mfmdev...@gmai l.comwrote:
                        Hi
                        I was wondering if there was a nicer way to swap the first 2
                        characters in a string with the 4th and 5th characters other than:
                        >
                        darr=list("0102 03040506")
                        aarr=darr[:2]
                        barr=darr[4:6]
                        darr[:2]=barr
                        darr[4:6]=aarr
                        result="".join( darr)
                        >
                        The above code works fine but I was wondering if anybody had another
                        way of doing this?
                        >
                        A
                        "4th and 5th characters" -darr[4:6]

                        You must be referring to the leading '0' as the 0th character, then.

                        -- Paul

                        Comment

                        Working...