replace string patern with different value

A

ajikoe

Hello,

I would like to replace string with different values,
For example :
source = 'kode1 bla bla kode1 bla kode1'
I have a list with each member will replace each of kode1.
L = [11,22,33]
So the new source will become:
newsource = '11 bla bla 22 bla 33'

How can I do it ? I tried to find using string built in function
replace, but it will replace pattern with the same value.

For this moment I think about change the string into list using
string.split and then check one by one and replace it and then convert
into string with addition space in the right and left of each element
list.

Is there any better idea ?

Pujo
 
B

Bill Mill

Hello,

I would like to replace string with different values,
For example :
source = 'kode1 bla bla kode1 bla kode1'
I have a list with each member will replace each of kode1.
L = [11,22,33]
So the new source will become:
newsource = '11 bla bla 22 bla 33'

How can I do it ? I tried to find using string built in function
replace, but it will replace pattern with the same value.

For this moment I think about change the string into list using
string.split and then check one by one and replace it and then convert
into string with addition space in the right and left of each element
list.
L = ['11', '22', '33']
source "xyzzy text we've got xyzzy text xyzzy yeah yeah yeah"
token 'xyzzy'
for rep in L:
.... source = source.replace(token, rep, 1)
...."11 text we've got 22 text 33 yeah yeah yeah"

And, if I may, I recommend the Python Tutorial at
http://docs.python.org/tut/tut.html .

Peace
Bill Mill
bill.mill at gmail.com
 
P

Peter Otten

I would like to replace string with different values,
For example :
source = 'kode1 bla bla kode1 bla kode1'
I have a list with each member will replace each of kode1.
L = [11,22,33]
So the new source will become:
newsource = '11 bla bla 22 bla 33'

Here's one way to do it with regular expressions:
.... items = iter(items)
.... def sub(m):
.... return items.next()
.... return sub
....
re.compile("kode1").sub(make_sub(str(i) for i in [11, 22, 33]),
.... "kode1 bla bla kode1 bla kode1")
'11 bla bla 22 bla 33'

Peter
 
F

Fredrik Lundh

Bill said:
... source = source.replace(token, rep, 1)

here's another way to do it:
L = ["11", "22", "33"]
S = "xyzzy text we've got xyzzy text xyzzy yeah yeah yeah"
L.reverse()
re.sub("xyzzy", lambda x: L.pop(), S)
"11 text we've got 22 text 33 yeah yeah yeah"

or, less destructive:
L = ["11", "22", "33"]
S = "xyzzy text we've got xyzzy text xyzzy yeah yeah yeah"
re.sub("xyzzy", lambda x, pop=iter(L).next: pop(), S)

(a few more iterations of this idea and we're in python riddle country...)

</F>
 
D

Duncan Booth

I would like to replace string with different values,
For example :
source = 'kode1 bla bla kode1 bla kode1'
I have a list with each member will replace each of kode1.
L = [11,22,33]
So the new source will become:
newsource = '11 bla bla 22 bla 33'

How can I do it ? I tried to find using string built in function
replace, but it will replace pattern with the same value.
source = 'kode1 bla bla kode1 bla kode1'
L = [11,22,33]
re.sub('kode1', (lambda m: str(L.pop(0))), source)
'11 bla bla 22 bla 33'
 
P

Paul McGuire

Well, not to be left out, here is a pyparsing solution. But this looks
suspiciously like some form of mail-merge or templating application.
Google for 'python templating' for existing approaches to this problem.

Interestingly, using an iterator of L sidesteps a number of other
problems. My original brute force version just used curelem as an
integer index into L, which required
1. a global declaration of curelem in replString
2. a separate variable to store the current element
3. a second command to increment curelem after saving the current
element
Changing curelem to be an iterator on the L list allowed me to collapse
all that junk down to a simple return statement, with no loss in
readability or maintainability.

-- Paul


from pyparsing import Literal
source = 'kode1 bla bla kode1 bla kode1'
L = [11,22,33]
curelem = iter(L)

def replString(st,loc,toks):
return str( curelem.next() )

kode = Literal("kode1").setParseAction( replString )
newsource = kode.transformString( source )
print newsource

prints:
11 bla bla 22 bla 33
 
B

Bengt Richter

Bill said:
... source = source.replace(token, rep, 1)

here's another way to do it:
L = ["11", "22", "33"]
S = "xyzzy text we've got xyzzy text xyzzy yeah yeah yeah"
L.reverse()
re.sub("xyzzy", lambda x: L.pop(), S)
"11 text we've got 22 text 33 yeah yeah yeah"

or, less destructive:
L = ["11", "22", "33"]
S = "xyzzy text we've got xyzzy text xyzzy yeah yeah yeah"
re.sub("xyzzy", lambda x, pop=iter(L).next: pop(), S)

(a few more iterations of this idea and we're in python riddle country...)
Another:
>>> L = ["11", "22", "33"]
>>> S = "xyzzy text we've got xyzzy text xyzzy yeah yeah yeah"
>>> ''.join([(i%2 and [n()] or [s()])[0] for s, n in
... [(iter(S.split('xyzzy')).next, iter(L).next)] for i in xrange(2*len(L)+1)])
"11 text we've got 22 text 33 yeah yeah yeah"

Or maybe:
"11 text we've got 22 text 33 yeah yeah yeah"

Regards,
Bengt Richter
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top