class People():
def __init__(self,name,replace_with):
self.name = name
self.replace_with = replace_with
self.first_encountered = False
def __str__(self):
return self.name+" -- "+str(self.first_encountered)
sentences = ["Rahul is eating an apple.",
"Rahul drinks milk.",
"Rahul also drinks Beer.",
"Rahul likes Pizza",
"Seema is going to the market",
"Seema also drinks beer",
"and i am going to hell"
]
names= ["Rahul", "Seema"]
replaces = ["He","She"]
people = [ People(n,r) for n,r in zip(names,replaces) ]
new_sentence = []
found_in_any = [False,False]
for sentence in sentences:
for index,person in enumerate(people):
if(sentence.find(person.name)!=-1):
found_in_any[index] = True
if(not person.first_encountered):
person.first_encountered = True
new_sentence.append(sentence)
continue
if(person.first_encountered):
new_sentence.append(sentence.replace(person.name,person.replace_with))
else:
found_in_any[index] = False
if len(list(set(found_in_any))) == 1 and list(set(found_in_any))[0] == False:
new_sentence.append(sentence)
print(new_sentence)
output : ['Rahul is eating an apple.',
'He drinks milk.',
'He also drinks Beer.',
'He likes Pizza',
'Seema is going to the market',
'Seema is going to the market',
'She also drinks beer',
'and i am going to hell']