Python报错:TypeError: 'dict_keys' object does not support indexing(机器学习实战treePlotter代码)解决方案
错误信息:
学习《机器学习实战》这本书时,按照书上的代码运行,产生了错误,但是在代码中没有错误提示,产生错误的代码如下:
firstStr = myTree.keys()[0]
print('树的叶子结点个数为:\n{}'.format(getNumLeafs(myTree)))
报错如下:
firstStr = myTree.keys()[0]
TypeError: 'dict_keys' object does not support indexing
把错误信息翻译一下:类型错误:dict_keys 对象不支持索引
错误产生是因为版本不同,作者使用的是2.x版本,而我使用的是3.7.x版本。
解决方案
对于3.x版本,因为python3改变了dict.keys,返回的是dict_keys对象,支持iterable 但不支持indexable,我们可以将其明确的转化成list
把错误代码修改成如下:
firstSides = list(myTree.keys())
firstStr = firstSides[0] # 找到输入的第一个元素
# or
firstStr = list(myTree.keys())[0] # 找到输入的第一个元素
此时运行就可以了。
参考文章:Here
Python报错:TypeError: 'dict_keys' object does not support indexing(机器学习实战treePlotter代码)解决方案的更多相关文章
- python 报错TypeError: 'range' object does not support item assignment,解决方法
贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints ...
- appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!
这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...
- python报错 TypeError: a() got multiple values for argument 'name'
[问题现象] 在一次调用修饰函数中出现了问题,折腾了一下午,一直报错 TypeError: got multiple values for argument 只是很简单的调用 from tsu2Ru ...
- pip install 报错 TypeError: 'module' object is not callable
$ pip install filetype Traceback (most recent call last): File "/usr/local/bin/pip2", line ...
- Python报错TypeError: '<' not supported between instances of 'str' and 'int'
n = input() if n>=100:print(int(n)/10) else:print(int(n)*10) 报错内容: Traceback (most recent call la ...
- selenium报错TypeError: 'FirefoxWebElement' object is not iterable
报错原因element少了s定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个s表示复数. 改为 返回结果为
- python报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0 解决方案
环境:mac+python 2.7 场景描述:在使用python修改excel内容修改表格内容为中文保存时报以下错误 此时已经设置了utf-8了 但保存时仍然报错错 此时将python中的中文使用un ...
- python框架Scrapy报错TypeError: 'float' object is not iterable解决
原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: pip3 install Twisted== 注:Twisted16..0安装后,会自动卸载高版本的Twi ...
- python报错“AttributeError: 'set' object has no attribute 'items'“
作为才开始学爬虫的萌新,遇到了一个这样的错,很懵逼 后面到网络到处查看大佬的解决方法,才发现headers的请求头部信息有错误,headers是一个字典,不是字符串,所以报错了 原代码 headers ...
- python报错 TypeError: string indices must be integers
所以在读取字典的时候,最好先判断类型,然后再查看它是否已经有这样的属性: type(mydict) == type({}) #检查不是字典 如果是字典,再看看有没有这样的属性: ...
随机推荐
- uni-app框架开发app发布流程
uni-app框架开发app发布流程 1.首先公司申请软著 步骤:申请软著详细流程 - 阿长*长 - 博客园 (cnblogs.com) 一.安卓端 1,点击发行>原生-app云打包 正式包和自 ...
- java文件读取 while ((len = reader.read(buffer)) != -1){}的理解
源文件 InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8"); //读 ...
- 吉特日化MES-业务架构第一版图
- [ABC282E] Choose Two and Eat One
Problem Statement A box contains $N$ balls, each with an integer between $1$ and $M-1$ written on it ...
- DataGrip给DateTime类型字段赋值当前系统默认时间
CURRENT_TIMESTAMP alter table 表名 modify column update_time DATETIME NULL DEFAULT CURRENT_TIMESTAMP O ...
- 推荐给前端开发的 5 款 Chrome 扩展 🚀
大家好,我是 dom 哥.这是我关于 Chrome 扩展开发的系列文章,感兴趣的可以 点个小星星. 工欲善其事,必先利其器.Chrome 可能是前端开发中使用最多的浏览器.在日常开发中,下列几款 Ch ...
- Python subprocess 使用(一)
Python subprocess 使用(一) 本文主要讲下 subprocess 的简单使用. 1: 通过subprocess 获取设备信息 import subprocess def get_an ...
- Python 潮流周刊第 33 期(摘要)
本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...
- uni-app+vue3+ts项目搭建完整流程
项目代码同步更新至码云 uni-vue3-ts-template 开发前准备 利用 uni-app 开发,有两种方法: 通过 HBuilderX 创建(需安装 HBuilderX 编辑器) 通过命令行 ...
- 5分钟就能实现的API监控,有什么理由不做呢?基调听云
API深度影响着你的应用 今天的数字应用世界其实是一个以API为中心的世界,我们只是没有意识到这些API的重要性.比如在电子商务交易.社交媒体等对交互高度依赖的领域,可以说API决定了应用的质量一点也 ...