The dictionary value mentioned below
dict = {'Incident, INC': 'Incident',
'SR, Service Request': 'Service Request',
'RCA ticket, Task': 'Problem',
'OCM, Alert': 'Change',
'Change': 'Minor enhancements'}
I need to map the dictionary values this
expect dictionary = {
'INC': 'Incident',
'Incident': 'Incident',
'Inc': 'Incident',
'Change': 'Minor Enchancement',
'SR': 'ServiceRequest',
'ServiceRequest': 'ServiceRequest'
}
I need to add more one condition given dictionary would the read given list if any of the value match it should the dict value or else it should return like Unspecified
.
input ---->
new_list= ['Incident','INC','SR','Change','ABC']
Expected output --->
new_list = ['Incident','Incident','Minor Enchancement','Service Request','others']
My code does not work.
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
CodePudding user response:
You can do the following:
d = {
"Incident, INC": "Incident",
"SR, Service Request": "Service Request",
"RCA ticket, Task": "Problem",
"OCM, Alert": "Change",
"Change": "Minor enhancements",
}
new_d = {}
for key, value in d.items():
keys = (item.strip() for item in key.split(","))
for k in keys:
new_d[k] = value
print(new_d)
output:
{'Incident': 'Incident',
'INC': 'Incident',
'SR': 'Service Request',
'Service Request': 'Service Request',
'RCA ticket': 'Problem',
'Task': 'Problem',
'OCM': 'Change',
'Alert': 'Change',
'Change': 'Minor enhancements'}
CodePudding user response:
Is this what you're looking for?
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] = dict[key]
print (rdict)
Returns:
{' Alert': 'Change',
' INC': 'Incident',
' Service Request': 'Service Request',
' Task': 'Problem',
'Change': 'Minor enhancements',
'Incident': 'Incident',
'OCM': 'Change',
'RCA ticket': 'Problem',
'SR': 'Service Request'}