python 如何使用逗号拆分字典值的键?

hkmswyz6  于 2022-12-21  发布在  Python
关注(0)|答案(1)|浏览(133)

下面提到的字典值

dict = {'Incident, INC': 'Incident',
 'SR, Service Request': 'Service Request',
 'RCA ticket, Task': 'Problem',
 'OCM, Alert': 'Change',
 'Change': 'Minor enhancements'}

我需要将字典值Map到

expect dictionary  = {
      'INC': 'Incident',  
      'Incident': 'Incident',
      'Inc': 'Incident', 
      'Change': 'Minor Enchancement',
      'SR': 'ServiceRequest',
      'ServiceRequest': 'ServiceRequest'
}

我需要添加多个条件给定字典将读取给定列表,如果任何值匹配它应该dict值,否则它应该返回像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
qlvxas9a

qlvxas9a1#

这就是你要找的吗?

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'}

相关问题