[英]Partial Matching a list of values to dictionary keys
我正在尝试清理 dataframe 的原始联系信息。 原始数据给出了一个人的头衔,我需要根据头衔确定资历。 如果标题与字典键有部分匹配,我需要将 append 该键的值添加到新列表中。 本质上,我需要遍历列表中的每个标题,看看是否存在与任何字典键的部分匹配,并将相应的字典值和 append 该值抓取到一个新列表中。 我尝试了多种格式的 for 循环和列表理解,但没有运气。
这是列表和字典的示例:
title = ['CEO', 'CFO', 'Financial Analyst', 'Associate', 'Tax Manager', 'Audit Manager']
seniority_dict = {'CEO':'Exec', 'CFO':'Exec', 'Manager':'Manager', 'Analyst':'Associate', 'Associate':'Associate'}
这是上面列表中相应值的资历
seniority = ['Exec', 'Exec', 'Associate', 'Associate', 'Manager', 'Manager']
如果你没有钥匙不在标题中
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.