I would like to sort and group a dictionary by keys. The keys are currently full names, but I would like to group all last names that are similar together and combine their value pairs. An excerpt of the input dictionary is below:
facdict = {'Yimei Li': [' Ph.D.', 'Assistant Professor of Biostatistics', 'liy3@email.chop.edu'],
'Mingyao Li': [' Ph.D.', 'Associate Professor of Biostatistics', 'mingyao@mail.med.upenn.edu'],
'Hongzhe Li': [' Ph.D', 'Professor of Biostatistics', 'hongzhe@upenn.edu'],
'A. Russell Localio': [' JD MA MPH MS PhD', 'Associate Professor of Biostatistics', 'rlocalio@upenn.edu']}
The desired output is:
last_name_dict = {'Li': [[' Ph.D.', 'Assistant Professor of Biostatistics', 'liy3@email.chop.edu'], [' Ph.D.', 'Associate Professor of Biostatistics', 'mingyao@mail.med.upenn.edu'], [' Ph.D', 'Professor of Biostatistics', 'hongzhe@upenn.edu']],
'Localio': [' JD MA MPH MS PhD', 'Associate Professor of Biostatistics', 'rlocalio@upenn.edu']}
I have tried to use the following dictionary comprehension:
search = re.compile(r"([A-Z]{1}[a-z]+)")
last_name_dict = {k.replace(k, search.findall(k)[-1:][0]): v for k, v in facdict.items()}
But this returns the last names of each entry with only the first value pair associated with it.