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({}) #检查不是字典 如果是字典,再看看有没有这样的属性: ...
随机推荐
- 微信小程序敏感内容检测
获取access_token access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存.access_token的存储至少要保 ...
- 小米二面:Redis 如何保证数据不丢失?
前段时间表妹收到了小米秋招补录的面试邀请,一面还算顺利,很快就通过了,但在看二面面试录屏的时候,我发现了一个问题,回答的不是很好,也就是我们今天要聊的这个问题:Redis 如何保证数据不丢失? 很多人 ...
- Mysql报:error while loading shared libraries libtinfo.so.5的解决办法
版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin #.今天闲来无事,想在Anolis8的系统上装一个MySQL8.0玩.前期在安装和配置的过程中没有什么问题,但是在我想 ...
- idea配置servlet项目找不到servlet jar包爆红【解决办法】
1.看你的implements 后面的Servlet是否大写了 2.大部分原因就是缺少servlet-api jar包或者idea找不到jar包 如果你是爆红的,那么问题就在这里,点击-号,重新添加这 ...
- notify为什么会引发超时,notify和notifyAll的区别
notify为什么会引发超时,notify和notifyAll的区别 每个同步对象都有对应的monitor,首先了解下monitor的内部结构. 1.monitor结构 Owner:指向拥有该同步对象 ...
- JavaWeb开发-CSS基础
2.CSS层叠样式表基本语法 层叠样式表,用来控制页面的样式 (1)CSS的三种引入方式 内部样式表:适合学习使用,将CSS代码写在style标签里面,style标签嵌套在title里 外部样式表:开 ...
- LeetCode5716:好因子的最大数目(数学、快速幂)
解题思路:因为primeFactors比较大,所以需要使用快速幂. class Solution: def quick_pow(self,base,x): ans = 1 while x>0: ...
- 获取微信的token工具类
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.http.HttpUtil; import cn.RedisUtil; impor ...
- 使用cgroup控制内存
关键文件 memory.limit_in_bytes memory.soft_limit_in_bytes memory.memsw.limit_in_bytes tasks cgroup.procs ...
- ElasticSearch之cat allocation API
查看各节点上各个shard的硬件使用情况,命令样例如下: curl -X GET "https://localhost:9200/_cat/allocation?v=true&pre ...