下面提到的字典值
dict = {'Incident, INC': 'Incident',
'SR, Service Request': 'Service Request',
'RCA ticket, Task': 'Problem',
'OCM, Alert': 'Change',
'Change': 'Minor enhancements'}
我需要映射字典值
expect dictionary = {
'INC': 'Incident',
'Incident': 'Incident',
'Inc': 'Incident',
'Change': 'Minor Enchancement',
'SR': 'ServiceRequest',
'ServiceRequest': 'ServiceRequest'
}
我需要添加更多的条件给定字典,读取给定列表,如果任何值匹配,它应该是字典值,否则它应该返回像Unspecified
。
输入——在
new_list= ['Incident','INC','SR','Change','ABC']
期望输出——>
new_list = ['Incident','Incident','Minor Enchancement','Service Request','others']
我的代码不工作。
reversed_dict = {}
for key in my_dict:
items = [x.strip() for x in my_dict[key].split()]
for i in items:
reversed_dict[i] = key
这是你要找的吗?
rdict = {}
for key in dict:
if any(list(map(lambda char: char in key, ','))):
k = key.split(',')
else: k = [key]
for i in k:
rdict[i.strip()] = dict[key]
print (rdict)
的回报:
{' Alert': 'Change',
' INC': 'Incident',
' Service Request': 'Service Request',
' Task': 'Problem',
'Change': 'Minor enhancements',
'Incident': 'Incident',
'OCM': 'Change',
'RCA ticket': 'Problem',
'SR': 'Service Request'}