第5章-11 字典合并 (40分)

字典合并。输入用字符串表示两个字典,输出合并后的字典,字典的键用一个字母或数字表示。注意:1和‘1’是不同的关键字!

输入格式:

在第一行中输入第一个字典字符串 在第二行中输入第二个字典字符串

输出格式:

在一行中输出合并的字典,输出按字典序。"1"的ASCII吗为49,大于1,排序时1在前,"1"在后,其它的也一样。

输入样例1:

在这里给出一组输入。例如:

{1:3,2:5}
{1:5,3:7} 
 

输出样例1:

在这里给出相应的输出。例如:

{1:8,2:5,3:7}
 

输入样例2:

在这里给出一组输入。例如:

{"1":3,1:4}
{"a":5,"1":6}
 

输出样例2:

在这里给出相应的输出。例如:

{1:4,"1":9,"a":5}

代码:
a = eval(input())
b = eval(input())
for i in b:
    if(a.get(i) == None):
        a[i] = b[i]
    else:
        a[i] += b[i]
list = []
for i in a:
    if(type(i) != int):
        list.append(ord(i))
    else:
        list.append(i)
list.sort()
flag = 0
print("{",end="")
for i in list:
    if(flag == 0):
        flag = 1
    else:
        print(",",end="")
    if(i < 10):
        print("%d:%d" % (i,a.get(i)),end="")
    else:
        print(""%s":%d" % (chr(i),a.get(chr(i))),end="")
print("}",end="")
原文地址:https://www.cnblogs.com/8023spz/p/13150874.html