用另一本词典的值替换一个词典的值

时间:2019-04-30 15:48:14

标签: python list dictionary

我想用字典2中的相应值替换字典1中的值

我知道字典和列表的理解在这里很有用,但是不确定如何在这种情况下应用它们。尤其令人困惑的是,有些值是列表格式的,但被视为字符串。

# Matches proteins to corresponding drug ids #
dict1 = {'Protein 1' : '001, 002, 003', 'Protein 2' : '003, 004', 'Protein 3' : '002'}

# Matches drug ids with drug names #
dict2 = {'001' : 'Drug1', '002' : 'Drug2', '003' : 'Drug3', '004' : 'Drug4'}

我想匹配dict2中的相应药物名称以替换dict 1中的药物编号,以便输出如下所示:

output_dict = {'Protein 1' : ['Drug1', 'Drug2', 'Drug3'], 'Protein 2' : ['Drug3', 'Drug4'], 'Protein 3' : ['Drug2']}

3 个答案:

答案 0 :(得分:3)

应事先更正输入词典(或由在上游产生该词典的程序),以使此答案更有意义。使用split进行简单的字典理解即可:

dict1 = {'Protein 1' : '001, 002, 003', 'Protein 2' : '003, 004', 'Protein 3' : '002'}

dict1 = {k : v.split(", ") for k,v in dict1.items()}

现在我们有了一个字符串列表作为值,这更有意义。

>>> dict1
{'Protein 1': ['001', '002', '003'],
 'Protein 2': ['003', '004'],
 'Protein 3': ['002']}

通过这种方式,只需使用翻译词典将词典重建为默认值即可恢复原始值,以避免键错误:

# Matches proteins to corresponding drug ids #
dict1 = {'Protein 1' : ['001', '002', '003'], 'Protein 2' : ['003', '004'], 'Protein 3' : ['002']}

# Matches drug ids with drug names #
dict2 = {'001' : 'Drug1', '002' : 'Drug2', '003' : 'Drug3', '004' : 'Drug4'}

dict3 = {k : [dict2.get(x,x) for x in v] for k,v in dict1.items()}

结果:

>>> dict3
{'Protein 1': ['Drug1', 'Drug2', 'Drug3'],
 'Protein 2': ['Drug3', 'Drug4'],
 'Protein 3': ['Drug2']}

答案 1 :(得分:1)

此代码将返回您要求的输出:

# Matches proteins to corresponding drug ids #
dict1 = {'Protein 1': ['001, 002, 003'], 'Protein 2': ['003, 004'], 'Protein 3': ['002']}

# Matches drug ids with drug names #
dict2 = {'001': 'Drug1', '002': 'Drug2', '003': 'Drug3', '004': 'Drug4'}


def string_replace_bulk(string, rules):
    result = string

    for old, new in rules.items():
        result = result.replace(old, new)

    return result


def weird_string_replace(src, rules):
    return [string_replace_bulk(s.strip(), rules) for s in src.split(',')]


def weird_list_replace(src, rules):
    result = {}
    for key, value in src.items():
        result[key] = []
        for item in value:
            result[key].extend(weird_string_replace(item, rules))

    return result


output_dict = weird_list_replace(dict1, dict2)

print(output_dict)

结果:

{'Protein 1': ['Drug1', 'Drug2', 'Drug3'], 'Protein 2': ['Drug3', 'Drug4'], 'Protein 3': ['Drug2']}

P.S。这真的是很奇怪的输入格式,最好用它来做些事情。

答案 2 :(得分:1)

单线:

>>> dict1 = {'Protein 1' : '001, 002, 003', 'Protein 2' : '003, 004', 'Protein 3' : '002'}
>>> dict2 = {'001' : 'Drug1', '002' : 'Drug2', '003' : 'Drug3', '004' : 'Drug4'}
>>> {k: map(dict2.get, v.split(", ")) for k, v in dict1.items()}
{'Protein 1': ['Drug1', 'Drug2', 'Drug3'], 'Protein 2': ['Drug3', 'Drug4'], 'Protein 3': ['Drug2']}

对于"001, ..."中代码dict1的每个列表,请分割字符串并将代码映射到dict2中的值