zoukankan      html  css  js  c++  java
  • 第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}
    第一版(代码死板,不建议参考,见第二版)
    # 字典合并
    # Author: cnRick
    # Time  : 2020-4-10
    dict1 = eval(input())
    dict2 = eval(input())
    keys1 = dict1.keys()
    keys2 = dict2.keys()
    minLenFlag = 1 if len(dict1) > len(dict2) else 0
    if minLenFlag == 0:
        for i in keys1:
            if i in dict2:
                dict2[i] = dict2[i] + dict1[i]
            else:
                dict2[i] = dict1[i]
        keys = list(dict2.keys())
        keys.sort(key = lambda x: ord(x) if type(x)==str else x)
        cnt = 0
        print("{",end="")
        for i in keys:
            if type(i) == int:
                print("{:d}:{:d}".format(i,dict2[i]),end="")
                cnt += 1
            elif type(i) == str:
                print('"{:s}":{:d}'.format(i,dict2[i]),end="")
                cnt += 1
            if cnt != len(dict2):
                print(",",end="")
        print("}",end="")
            
    else:
        for i in keys2:
            if i in dict1:
                dict1[i] = dict1[i] + dict2[i]
            else:
                dict1[i] = dict2[i]
        keys = list(dict1.keys())
        keys.sort(key = lambda x: ord(x) if type(x)==str else x)
        cnt = 0
        print("{",end="")
        for i in keys:
            if type(i) == int:
                print("{:d}:{:d}".format(i,dict1[i]),end="")
                cnt += 1
            elif type(i) == str:
                print('"{:s}":{:d}'.format(i,dict1[i]),end="")
                cnt += 1
            if cnt != len(dict1):
                print(",",end="")
        print("}",end="")

    第二版

     1 # 字典合并-简化版
     2 # Author: cnRick
     3 # Time  : 2020-4-10
     4 dict1 = eval(input())
     5 dict2 = eval(input())
     6 for key in dict2.keys():
     7     dict1[key] = dict1.get(key,0) + dict2[key]
     8 
     9 items_list = list(dict1.items())
    10 items_list.sort(key=lambda item:ord(item[0]) if type(item[0]) == str else item[0])
    11 print(str(dict(items_list)).replace(" ","").replace("'",'"'))

    思路借鉴:https://blog.csdn.net/qq_43479432/article/details/105009411

     
  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    《EffectiveJava中文第二版》 高清PDF下载
    《MoreEffectiveC++中文版》 pdf 下载
    《啊哈c语言》 高清 PDF 下载
  • 原文地址:https://www.cnblogs.com/dreamcoding/p/12676478.html
Copyright © 2011-2022 走看看