String replacement in Python is not difficult, but I want to do something special:
teststr = 'test test test test'
animals = ['bird','monkey','dog','fox']
#replace 'test' with random item from animals
finalstr = ['dog fox dog monkey']
I write a very inefficient version:
from random import choice
import string
import re
teststr = 'test test test test'
animals = ['bird','monkey','dog','fox']
indexes = [m.start() for m in re.finditer('test', 'test test test test')]
#indexes = [0, 5, 10, 15]
for i in indexes:
string.replace(teststr, 'test', choice(animals), 1)
#Final result is four random animals
#maybe ['dog fox dog monkey']
It works, but I believe there is some simple method with REGULAR EXPRESSION which I am not familiar with.