0

In the below mentioned code whenever I am replacing the word ROAD with RD., then word BROAD is also getting replaced to 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
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219

0 Answers0