Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace a word in an address string with dictionary value using for-loop

I have an address 2300 S SUPER TEMPLE PL which I expect to get 2300 S SUPER TEMPLE PLACE as a result after spelling out the PL to PLACE. I have a dictionary of abbreviated street names:

st_abbr = {'DR': 'DRIVE',
            'RD': 'ROAD', 
            'BLVD':'BOULEVARD',
            'ST':'STREET', 
            'STE':'SUITE',
            'APTS':'APARTMENTS', 
            'APT':'APARTMENT',
            'CT':'COURT',
            'LN' : 'LANE',
            'AVE':'AVENUE',
            'CIR':'CIRCLE',
            'PKWY': 'PARKWAY',
            'HWY': 'HIGHWAY',
            'SQ':'SQUARE',
            'BR':'BRIDGE',
            'LK':'LAKE',
            'MT':'MOUNT',
            'MTN':'MOUNTAIN',
            'PL':'PLACE',
            'RTE':'ROUTE',
            'TR':'TRAIL'}

with a for-loop, I would like to replace the key in address be spelled out. What I thought I should do is loop through each word in the address, thus I have the address.split(), and if the split match one of the keys in the dictionary, to replace that with a spelled out word.

for key in st_abbr.keys():
        if key in address.split():
            address = address.replace(key, st_abbr[key])
        print(address)

It works perfectly on abbreviated street names but this is what I get 2300 S SUPER TEMPLACEE PLACE. It also replaced the PL within ‘TEMPLE’ with PLACE, thus it gave me ‘TEMPLACEE’. I am trying to modify the for loop to only replace the abbreviated street if the street.split() is the exact match of the dict.keys(). I would like guidance on how to achieve that.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use a comprehension:

addr = '2300 S SUPER TEMPLE PL'
new_addr = ' '.join(st_abbr.get(c, c) for c in addr.split())
print(new_addr)

# Output
2300 S SUPER TEMPLE PLACE

Can you shed a light the concept behind the .get(c,c) in the context of my problem?

# Equivalent code
' '.join(st_abbr[c] if c in st_abbr else c for c in addr.split())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading