I have the following two lists:
input = ['MAPLEWOOD AVE', 'LYNNDALE ', 'SUGAR DR']
ref = ['LYNNDALE (?:RD)?', 'HOMAN (?:AVE)?', 'MAPLEWOOD (?:AVE)?', 'LYNNDALE (?:LN)?']
I would like to look for all matches for each element within input with ref. The output would be a dictionary with each key being an input element, and each value being a ref element matched to the corresponding input element, like the following:
{'MAPLEWOOD AVE' : 'MAPLEWOOD AVE', 'LYNNDALE ' : 'LYNNDALE RD', 'LYNNDALE LN', 'SUGAR DR':}
The following allows me to iterate over input in search of a findall match within ref (which contains embedded regex groupings). However, I cannot retrieve the corresponding match element(s) from ref as values alongside each input element:
combined = "(" + ")|(".join(ref) + ")"
l = []
for i in input:
if re.findall(combined,i):
l.append(i)
...
MAPLEWOOD AVE
LYNNDALE
Try:
Or if you’re using Python 3:
Also you could compile your regexs to speed them up a little:
UPD:
Maybe you want to use matched part as values, not patterns: