美文网首页
Python字典实现健值合并和给字典添加对应健的值

Python字典实现健值合并和给字典添加对应健的值

作者: KangSmit的算法那些事儿 | 来源:发表于2020-06-27 13:15 被阅读0次

    如下图所示,顺便说一下可能的报错问题:


    image.png

    问题:

    TypeError: 'dict' object is not callable
    

    原因:

    dict()是python的一个内建函数,如果将dict自定义为一个python字典,在之后想调用dict()函数是会报出“TypeError: 'dict' object is not callable”的错误,只需将之前自定义的变量delete掉即可。

    解决办法:

    >>>del (dict)
    

    修改之后

    image.png

    小项目:如何给《三国演义》中的部分名称编号

    #给关键字添加对应值
    #del (dict)这里无需删除字典内置函数,因为下面已经定义好了
    excludes={"将军","却说","二人","后主","上马","不知","天子","大叫","众将","不可",
              "主公","蜀兵","只见","如何","商议","都督","一人","汉中","不敢","人马",
              "陛下","魏兵","天下","今日","左右","东吴","于是","荆州","不能","如此",
              "大喜","引兵","次日","军士","军马"}  
    
    keys=[]
    values=[]
    for i in range(len(excludes)):
        values.append(i+1)
    for i in excludes:
        keys.append(i)
           
    dictionary = dict(zip(keys, values))
    
    print(dictionary)
    
    
    image.png

    相关文章

      网友评论

          本文标题:Python字典实现健值合并和给字典添加对应健的值

          本文链接:https://www.haomeiwen.com/subject/afwmfktx.html