INPUT:
dict1 = {'hi':45,'thanks':34,'please':60}
dict2 = {'hi':40,'thanks':46}
输出:
DIC = {'hi':5,'thanks': - 12,'please':60}
怎么样?请给我回答......答案 0 :(得分:0)
因此,对于你想要的dict1的每个键:
dict1[key] - dict2[key]
dict1[key]
我是对的吗?
解决方案:使用for
循环
dict1 = {'hi' : 45, 'thanks' : 34, 'please' : 60}
dict2 = {'hi' : 40, 'thanks' : 46}
output_dict = {}
for key in dict1.keys() :
if key in dict2.keys() :
output_dict[key] = dict1[key] - dict2[key]
else :
output_dict[key] = dict1[key]