在做《Machine Learning in Action》书中的第三章绘制树形图时遇到了这个问题AttributeError: type object 'str' has no attribute '_name_'

很明显是if type(secondDict[key])._name_  == ‘dict’:这一句有问题,在python3中并没有type(secondDict[key])._name_这样的用法

所以我们要将源代码中的_name_去掉,变成if type(secondDict[key])  == ‘dict’:但这时我们会发现新的问题,编译确实没有报错,但是结果却和我们想要的结果不一样

我们很容易知道叶子节点的数目为3,但我们得出的结果却是2,这是怎么回事呢?我们再回到源码中看

if type(secondDict[key])  == ‘dict’:

这一句中的'dict'加了引号,这就是问题所在了,我们这句判断的语句本来是判断该节点类型是否为dict(字典),但是现在却成了判断该节点类型和‘dict'的类型是否相同,很明显'dict'的类型为string类型,故而,我们本来是想求得一颗树中的叶子节点的数目,现在却变成了求树节点中除叶子节点的其他节点的数目。故而输出2

现在我们知道问题所在,修改就简单了。两种改法:

if type(secondDict[key])  == dict:

或者if type(secondDict[key])  is dict:

再测试下,成功!

python 提示错误AttributeError: type object 'str' has no attribute '_name_'相关推荐

  1. python提示错误TypeError: 'dict_keys' object does not support indexing

    python提示错误TypeError: 'dict_keys' object does not support indexing 原因:python2与python3之间的差异 python2中形如 ...

  2. python object has no attribute_如何修复python中的“AttributeError:type object has no attribute”?...

    您的代码引发此异常: AttributeError: type object 'Meeting' has no attribute 'datetime' 在这一行: meeting_start = M ...

  3. ubuntun 16.04 AttributeError: type object 'Callable' has no attribute '_abc_registry'

    ubuntun 16.04 python 升级到3.7遇到各种坑 其中在openvino 跑模型时遇到以下错误: root@magicdepth-OptiPlex-7010:/opt/intel/op ...

  4. “AttributeError: type object ‘RocCurveDisplay‘ has no attribute ‘from_predictions‘ “.

    "AttributeError: type object 'RocCurveDisplay' has no attribute 'from_predictions' ". 目录 & ...

  5. 关于AttributeError: type object ‘XXX‘ has no attribute ‘XXX‘的问题

    关于AttributeError: type object 'XXXXXX' has no attribute 'name'的报错的原因,先运行运行一段代码 例如: class Person(obje ...

  6. AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘

    错误描述: Traceback (most recent call last):File "D:\OCR\DBNet.pytorch-master\tools\predict.py" ...

  7. AttributeError: type object ‘Image‘ has no attribute ‘open‘

    AttributeError: type object 'Image' has no attribute 'open' 原因分析:Image调用顺序出错,因为第一行的from PIL import I ...

  8. unittest-ddt报错AttributeError: type object ‘forTestDDT‘ has no attribute ‘test_2‘

    unittest 添加多个ddt数据驱动后,报错: FAILED (errors=1)Error Traceback (most recent call last):File "D:\Ana ...

  9. python 报错 AttributeError: type object ‘datetime.datetime‘ has no attribute ‘datetime‘

    python报错:type object 'datetime.datetime' has no attribute 'datetime' 描述:在第一个python程序里还未报错,第二个程序完全复制过 ...

最新文章

  1. JetBrains 2018 中国开发者生态报告:Java 最流行
  2. 鸿蒙系统代码开源不担忧友商,华为鸿蒙系统正式开源!打脸“安卓套皮论”,友商手机也可采用!...
  3. c++中的引用和python中的引用_对比 C++ 和 Python,谈谈指针与引用
  4. 按实际价格重估在版本 0, 财政年度 2016 中不可能
  5. Spring第八发—自动装配及让Spring自动扫描和管理Bean
  6. 1.设计模式中监听模式(观察者模式)(Python实现)
  7. 分享珍藏很久的Python学习知识手册
  8. 6大设计原则之开闭原则
  9. LintCode: Median of two Sorted Arrays
  10. 体检异常率98%?数据分析告诉你如今的90后身体状况到底有多差?
  11. 医疗保健行业未来发展的5大趋势预测
  12. wallys/IPQ4019/IPQ4029/Access Point Wireless Module Dual band 11AC Wave2 Module
  13. Swift3.0 中实现发短信功能
  14. 计算机中我的云文档在哪儿里,wps云文档可以删除吗在哪里删除
  15. iOS 模拟器 获取位置 设置自定义位置
  16. Cadence中的电路设计
  17. 1 Microservice 简介
  18. 为什么要上机械制造业ERP系统?对企业有什么帮助?
  19. springcloud实战演练pdf_基于 Spring Cloud 的微服务架构实践指南(上)
  20. JavaScript实战手册(涵盖丰富的JQuery应用实例)

热门文章

  1. mysql webhook_sql 数据定时发送webhook golang 服务
  2. python语法错误修改_python基础之错误、调试(异常处理)
  3. mybatis-plus设置查询返回的类型是map
  4. Google Maps——页面提示[For development purposes only]解决方案
  5. 《数据结构与算法》课程设计任务书——赫夫曼编码/译码器
  6. CG CTF WEB 单身一百年也没用
  7. 暴走英雄坛html5游戏在线玩,暴走英雄坛h5
  8. springboot使用@Scheduled作定时任务详细用法
  9. 【学习笔记】比较分别用prim和kruskal实现最小生成树和算法优化方案
  10. HTTP请求返回状态码和提示信息