根据字典内的键值修改另一个字典的value

需求:根据传入字典的key1,将value1循环替换到已知字典内相同key的value

def dispose_dict(input_parameter,fixed_parameter):
    for input_key, input_value in input_parameter.items():
        for fixed_key, fixed_value in fixed_parameter.items():
            if input_key == fixed_key:
                if isinstance(fixed_parameter[fixed_key],str):
                    fixed_parameter[fixed_key] = input_value
                    break
                elif isinstance(fixed_parameter[fixed_key],list):
                    if len(fixed_parameter[fixed_key]) == 0:
                        fixed_parameter[fixed_key].append(input_value)
                        break
            elif isinstance(fixed_value, dict):
                if len(fixed_value) != 0:
                    dispose_dict(input_parameter,fixed_value)
            elif isinstance(fixed_value, list):
                if len(fixed_value) != 0:
                    if isinstance(fixed_value[0], dict):
                        for kv1 in fixed_value:
                            dispose_dict(input_parameter, kv1)

    return fixed_parameter

input_parameter = {“k1”:"v1","k2":"v2"}
fixed_parameter = {"k1":"嘻嘻","k3":{"k2":"哈哈"}}

return fixed_parameter:
{"k1":"v1","k3":{"k2":"v2"}}

 

posted @ 2020-08-17 17:17  李有病  阅读(578)  评论(0编辑  收藏  举报
1 2