在下面提到的代码中,每当我用RD。替换单词ROAD时,那么单词BROAD也会被替换为BRD。
def subst(pattern, replace_str, string):
#new_string = [word.replace(pattern,replace_str) for word in string]
new_string=[]
for word in string:
if re.match(pattern, word):
new_string.append(word.replace(pattern,replace_str))
else:
new_string.append(word)
#susbstitute pattern and return it
return new_string
def main():
addr = ['100 NORTH MAIN ROAD',
'100 BROAD ROAD APT.',
'SAROJINI DEVI ROAD',
'BROAD AVENUE ROAD']
#Create pattern Implementation here
#Use subst function to replace 'ROAD' to 'RD.',Store as new_address
new_address= subst('ROAD','RD.',addr)
return new_address