Replace substring with multiple words in python3

Question:

I am trying to replace a word in a string with multilple words and produce all the strings as an output.

For example ‘disease’ in ‘lysosome storage disease’ should be replaced by ‘disease’ , ‘diseases’, ‘disorder’, ‘disorders’,’syndrome’,’syndromes’ and produce following output.

lysosome storage disease
lysosome storage diseases
lysosome storage disorder
lysosome storage disorders
lysosome storage syndrome
lysosome storage syndromes

I am trying following lines of code but, in the end I am getting only the last string.

def multiple_replace(string, rep_dict):
    pattern = re.compile("|".join([re.escape(k) for k in sorted(rep_dict,key=len,reverse=True)]), flags=re.DOTALL)
    return pattern.sub(lambda x: rep_dict[x.group(0)], string)

multiple_replace("lysosome storage disease", {'disease':'disease', 'disease':'diseases', 'disease':'disorder', 'disease':'disorders','disease':'syndrome','disease':'syndromes'})
Asked By: rshar

||

Answers:

Try the follow:

t = 'lysosome storage disease' 

for r in ('disease' , 'diseases', 'disorder', 'disorders','syndrome','syndromes'):
   print(t.replace('disease',r))

# output
lysosome storage disease
lysosome storage diseases
lysosome storage disorder
lysosome storage disorders
lysosome storage syndrome
lysosome storage syndromes

You can improve with Template from https://docs.python.org/3/library/string.html

Answered By: Franz Kurt

You are getting the last string because it is a dictionary and dictionary does not have duplicate keys.

You can do this instead :

rep_dict={'disease':['disease' , 'diseases', 'disorder', 'disorders','syndrome','syndromes']} 
# this is changed, I have put all the values in the list


def multiple_replace(string, rep_dict):
    pattern = re.compile("|".join([re.escape(k) for k in sorted(rep_dict,key=len,reverse=True)]), flags=re.DOTALL)
    return pattern.sub(lambda x: rep_dict[x.group(0)], string)


for x in range(len(rep_dict['disease'])):
    print(multiple_replace("lysosome storage disease", {'disease':rep_dict['disease'][x]}))

#output
lysosome storage disease
lysosome storage diseases
lysosome storage disorder
lysosome storage disorders
lysosome storage syndrome
lysosome storage syndromes
Answered By: God Is One
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.