[英]Partial Matching a list of values to dictionary keys
I am trying to clean a dataframe of raw contact information.我正在尝试清理 dataframe 的原始联系信息。 The raw data gives a person's title and based on the title I need to determine a level of seniority.
原始数据给出了一个人的头衔,我需要根据头衔确定资历。 If the title has a partial match to the dictionary key, I need to append the value for that key to a new list.
如果标题与字典键有部分匹配,我需要将 append 该键的值添加到新列表中。 Essentially I need to iterate through each title in the list and see if there is a partial match to any dictionary key and grab the corresponding dictionary value and append that value to a new list.
本质上,我需要遍历列表中的每个标题,看看是否存在与任何字典键的部分匹配,并将相应的字典值和 append 该值抓取到一个新列表中。 I've tried multiple formats of for loops and list comprehension but no luck.
我尝试了多种格式的 for 循环和列表理解,但没有运气。
Here's an example of the list and dict:这是列表和字典的示例:
title = ['CEO', 'CFO', 'Financial Analyst', 'Associate', 'Tax Manager', 'Audit Manager']
seniority_dict = {'CEO':'Exec', 'CFO':'Exec', 'Manager':'Manager', 'Analyst':'Associate', 'Associate':'Associate'}
Here's what the seniority should be for the corresponding values in the list above这是上面列表中相应值的资历
seniority = ['Exec', 'Exec', 'Associate', 'Associate', 'Manager', 'Manager']
In case you don't have key not in title如果你没有钥匙不在标题中
title = ['Software Engineer', 'CEO', 'CFO', 'Financial Analyst', 'QA Engineer', 'Associate', 'Tax Manager', 'Audit Manager']
seniority_dict = {'CEO': 'Exec', 'CFO': 'Exec', 'Manager': 'Manager', 'Analyst': 'Associate', 'Associate': 'Associate'}
new_list = []
for t in title:
found = False
for key, value in seniority_dict.items():
if key in t:
new_list.append(value)
found = True
break
if not found:
new_list.append('NaN')
print(new_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.