I have the following string:
oldstring = 'michael smith passes both danny jones III and michael robinson on turn 3!'
I'd like to use the oldstring
above and the racer_dict
below (or a better solution) to create the newstring
below.
name = ['michael smith sr', 'darrel michael robinson', 'danny jones III']
racing_number = ['44', '15', '32']
racer_dict = dict(zip(name, racing_number))
newstring = '44 passes both 32 and 15 on turn 3!'
It's a complicated problem because, as in the example:
- sometimes the name being replaced completely matches the
racer_dict
key - the word length of the names being replaced are not consistent
- the same word can show up in two different drivers names (however, I wouldn't expect the same two words to show up in two different drivers' names).
Below is the solution I've come-up with on my own, but seems a bit cumbersome:
# Replace the name in oldstring when it matches the exact name in the dict
old_ones = [x for x in name if x in oldstring]
newstring = oldstring
if len(old_ones) > 0:
for old in old_ones:
newstring = re.sub(old, racer_dict.get(old), newstring)
# Now look for when two consecutive words from oldstring are found in the
# dict name, and replace them too
name_strings = []
name_numbers = []
nsw = newstring.split(' ')
for i in range(len(nsw)-1):
potential_name = nsw[i] + ' ' + nsw[i+1]
key_name = [x for x in name if potential_name in x]
if len(key_name) > 0:
value_number = racer_dict.get(key_name[0])
name_strings.append(potential_name)
name_numbers.append(value_number)
if len(name_strings) > 0:
replacers = dict(zip(name_strings, name_numbers))
for j in name_strings:
newstring = re.sub(j, replacers.get(j), newstring)
print(newstring)
# 44 passes both 32 and 15 on turn 3!