I am trying to replace the variables with placeholders like XXX. The words "hello" and "morning" are printed as-is because they appear in another list. The following code works, but prints extra placeholders.
import re
mylist = ['hello', 'morning']
nm = [
"Hello World Robot Morning.",
"Hello Aniket Fine Morning.",
"Hello Paresh Good and bad Morning.",
]
def punctuations(string):
pattern = re.compile(r"(?u)\b\w\w+\b")
result = pattern.match(string)
myword = result.group()
return myword
for x in nm:
newlist = list()
for y in x.split():
for z in mylist:
if z.lower() == punctuations(y.lower()):
newlist.append(y)
else:
newlist.append("xxx")
print(newlist)
Output:
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
Expected output:
['Hello', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']