Re: replace string patern with different value
On Mon, 9 May 2005 15:48:14 +0200, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: >Bill Mill wrote: > > for rep in L: >> ... 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: >>> ''.join(map(lambda x,y:(x or '')+(y or ''), S.split('xyzzy'), L)) "11 text we've got 22 text 33 yeah yeah yeah" Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: replace string patern with different value
Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: replace string patern with different value
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: replace string patern with different value
Bill Mill wrote: for rep in L: > ... 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...) -- http://mail.python.org/mailman/listinfo/python-list
Re: replace string patern with different value
[EMAIL PROTECTED] wrote: > 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' -- http://mail.python.org/mailman/listinfo/python-list
Re: replace string patern with different value
[EMAIL PROTECTED] wrote: > 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: >>> def make_sub(items): ... 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 -- http://mail.python.org/mailman/listinfo/python-list
replace string patern with different value
On 9 May 2005 06:23:41 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > 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) ... >>> source "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 -- http://mail.python.org/mailman/listinfo/python-list
replace string patern with different value
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 -- http://mail.python.org/mailman/listinfo/python-list