浙大版《Python 程序设计》题目集 第5章-11 字典合并 (40分)

d1 = eval(input())
d2 = eval(input())
d_str = {}
d_digit = {}
d_final = {}
for i in d1:
    if type(i)==int:
        d_digit[i] = d1[i]
    elif type(i)==str:
        d_str[i] = d1[i]
for j in d2:
    if type(j)==int :
        d_digit[j] = d_digit.get(j,0) + d2[j]
    elif type(j)==str:
        d_str[j] = d_str.get(j,0) + d2[j]
str_list = list(d_str.keys())
digit_list = list(d_digit.keys())
str_list.sort()
digit_list.sort()
for a in digit_list:
    d_final[a] = d_digit[a]
for b in str_list:
    d_final[b] = d_str[b]

print('{',end='')
count = 0
for key,value in d_final.items():
    count += 1
    if type(key)==int:
        print('{}:{}'.format(key,value),end='')
    elif type(key)==str:
        print('"{}":{}'.format(key,value),end='')
    if count != len(d_final):
        print(',',end='')
print('}')

你可能感兴趣的:(PTA)