replacing substrings within strings

Discussion in 'Python' started by amadain, Feb 14, 2007.

  1. amadain

    amadain Guest

    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("010203040506")
    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
    amadain, Feb 14, 2007
    #1
    1. Advertising

  2. 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("010203040506")
    > 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("010203040506")
    darr[:2], darr[4:5] = darr[4:6], darr[:2]
    result="".join(darr)
    print result


    Diez
    Diez B. Roggisch, Feb 14, 2007
    #2
    1. Advertising

  3. amadain

    Rune Strand Guest

    On Feb 14, 1:08 pm, "amadain" <> wrote:
    > 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("010203040506")
    > 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:])
    Rune Strand, Feb 14, 2007
    #3
  4. amadain

    amadain Guest

    On Feb 14, 12:16 pm, "Diez B. Roggisch" <> wrote:
    > 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("010203040506")
    > > 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("010203040506")
    > 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
    amadain, Feb 14, 2007
    #4
  5. Le mercredi 14 février 2007 13:08, amadain a écrit :
    > darr=list("010203040506")
    > 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
    Maric Michaud, Feb 14, 2007
    #5
  6. amadain <> wrote:
    >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("010203040506")
    >aarr=darr[:2]
    >barr=darr[4:6]
    >darr[:2]=barr
    >darr[4:6]=aarr
    >result="".join(darr)


    darr=list("010203040506")
    darr[:2], darr[4:6] = darr[4:6], darr[:2]
    result = "".join(darr)

    --
    \S -- -- 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
    Sion Arrowsmith, Feb 14, 2007
    #6
  7. amadain

    Rune Strand Guest

    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:]
    Rune Strand, Feb 14, 2007
    #7
  8. > 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
    Diez B. Roggisch, Feb 14, 2007
    #8
  9. amadain

    Guest

    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", "010203040506")
    >>> 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
    , Feb 14, 2007
    #9
  10. amadain

    amadain Guest

    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
    amadain, Feb 14, 2007
    #10
  11. 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
    Diez B. Roggisch, Feb 14, 2007
    #11
  12. amadain

    Paul McGuire Guest

    On Feb 14, 6:08 am, "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("010203040506")
    > 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
    Paul McGuire, Feb 14, 2007
    #12
    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. Will McGugan

    Replacing large number of substrings

    Will McGugan, Sep 4, 2005, in forum: Python
    Replies:
    3
    Views:
    321
    Michael J. Fromberger
    Sep 4, 2005
  2. Tung Chau
    Replies:
    1
    Views:
    462
    SM Ryan
    Aug 6, 2004
  3. Tung Chau
    Replies:
    0
    Views:
    367
    Tung Chau
    Aug 6, 2004
  4. DarthBob88

    Finding and Replacing Substrings In A String

    DarthBob88, Sep 23, 2007, in forum: C Programming
    Replies:
    7
    Views:
    553
    Keith Thompson
    Sep 23, 2007
  5. Karsten Wutzke
    Replies:
    3
    Views:
    382
    Jeff Higgins
    Mar 20, 2008
Loading...

Share This Page