fork download
  1. import re
  2. strings = ['corpus christi tx', 'san angelo', 'oklahoma city ok', 'abilenesweetwater']
  3. lookup = {'tx': 'texas', 'ny': 'new york', 'nj': 'new jersey', 'ok': 'oklahoma'}
  4. rx = re.compile(fr'\b(?:{"|".join([key for key in lookup])})\b')
  5. strings = [rx.sub(lambda x: lookup[x.group()], s) for s in strings]
  6. print(strings)
Success #stdin #stdout 0.03s 9512KB
stdin
Standard input is empty
stdout
['corpus christi texas', 'san angelo', 'oklahoma city oklahoma', 'abilenesweetwater']