On Feb 14, 6:08 am, "amadain" <[EMAIL PROTECTED]> 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=
[EMAIL PROTECTED] 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
--
http://mail.python.org/mailman/listinfo/python-list
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
--
http://mail.pytho
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 su
> 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
--
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:]
--
http://mail.python.org/mailman/listinfo/python-list
amadain <[EMAIL PROTECTED]> 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("010203040
On Feb 14, 12:16 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> 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]
> >
On Feb 14, 1:08 pm, "amadain" <[EMAIL PROTECTED]> 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="".jo
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
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
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
12 matches
Mail list logo