Python合并多个字典


有两个字典,形如


 a = {"key": "[email protected]"}
b = {"key": "[email protected]"}

如何根据相同的key将a,b合并成一个字典?如:


 c = {"key": "[email protected],[email protected]"}
或者
c = {"key": "[[email protected], [email protected]]"}

python dict

三山五岳丸 9 years, 2 months ago

不知道下面的代码符不符合你的要求


 dic1={"name":"paul","age":25};
dic2={"name":"mark","age":23};

dic={};

for key in dic1:
    if dic2[key] is not None:
        dic[key]=str(dic1[key])+","+str(dic2[key]);
    else:
        dic[key]=dic1[key];

for key in dic2:
    if dic[key] is None:
        dic[key]=dic2[key];

print(str(dic))

幸福的小帐篷 answered 9 years, 2 months ago

Your Answer