replacing substrings within strings

A

amadain

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
 
D

Diez B. Roggisch

amadain said:
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
 
R

Rune Strand

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:])
 
A

amadain

amadain said:
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
 
M

Maric Michaud

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
 
S

Sion Arrowsmith

amadain said:
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)
 
R

Rune Strand

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:]
 
D

Diez B. Roggisch

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
 
B

bearophileHUGS

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
 
A

amadain

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
 
P

Paul McGuire

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top