replace string patern with different value

Duncan Booth duncan.booth at invalid.invalid
Mon May 9 09:52:40 EDT 2005


ajikoe at gmail.com 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'



More information about the Python-list mailing list