Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
引言
目前Python2和Python3存在版本上的不兼容性,这里将列举dict中的问题之一。下面话不多说,来看看详细的介绍:
1. Python 2 vs python 3
根据Python社区的主流要求,Python 2将在最近的若干年内不再提供技术支持,目前的python 2.7.12已经是其维护版本;如无意外,大家请参照使用Python 3.
Python 3与Python 2之间的割裂以及向下不兼容性是其一个非常著名的事件,给整个社区和相关应用造成了相当的困扰。
2. 问题
Python 2:
>> dict1 = {}
>> dict1['key1'] = 'val1'
>> for k, v in dict1.items():
print(k+ "=>" + v)
Traceback (most recent call last): File "<ipython-input-23-5ccef53f3d75>", line 1, in <module> hist_sorted = sorted(hist.iteritems(), key=lambda d: d[1], reverse=True) AttributeError: 'dict' object has no attribute 'items'
3. 问题的解决
确认其为Python 3下的用法:
在Python 2之下的用法是:
for k, v in dict1.iteritems(): print(k+ "=>" + v)
上述代码在python3下,可以正确的执行运行。
4. Python 2 vs python 3
从Python 2向Python 3的进化需要一个过程,其中的差异可以参照如下文章:http://www.jb51.net/article/105518.htm
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。
相关推荐
-
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
引言 目前Python2和Python3存在版本上的不兼容性,这里将列举dict中的问题之一.下面话不多说,来看看详细的介绍: 1. Python 2 vs python 3 根据Python社区的主流要求,Python 2将在最近的若干年内不再提供技术支持,目前的python 2.7.12已经是其维护版本:如无意外,大家请参照使用Python 3. Python 3与Python 2之间的割裂以及向下不兼容性是其一个非常著名的事件,给整个社区和相关应用造成了相当的困扰. 2. 问题 Pyt
-
关于pycharm 切换 python3.9 报错 ‘HTMLParser‘ object has no attribute ‘unescape‘ 的问题
有的bug,莫名其妙就好了- python3.9 报错 "AttributeError: 'HTMLParser' object has no attribute 'unescape'" 异常分析解决. 一.问题描述 安装 python3.9 版本后,pycharm 中切换 python3.9 版本,创建虚拟环境报错:"AttributeError: 'HTMLParser' object has no attribute 'unescape'". Executed
-
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8 查看目前系统字符集 复制代码 代码如下: import sys print sys.getdefaultencoding() 执行: 复制代码 代码如下: [root@lee ~]# python a.py ascii 修改成utf-8 复制代码 代码如下: import sys sys.setdefaultencoding('utf-8') print sys.get
-
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8 查看目前系统字符集 复制代码 代码如下: import sys print sys.getdefaultencoding() 执行: 复制代码 代码如下: [root@lee ~]# python a.py ascii 修改成utf-8 复制代码 代码如下: import sys sys.setdefaultencoding('utf-8') print sys.get
-
解决AttributeError: ‘NoneType‘ object has no attribute ‘Window‘的问题(亲测有效)
目录 报错问题 报错翻译 报错原因 已解决VSCode运行强化学习代码抛出异常AttributeError: ‘NoneType’ object has no attribute ‘Window’pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to “None” 的正确解决方法,亲测有效!! 报错问题 一个小伙伴遇到问题跑来私信我,在VSCode运行强化学习代码,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮
-
解决运行出现'dict' object has no attribute 'has_key'问题
这个问题是py2和py3兼容性的问题 在py2中,判断key是否属于dict的写法可以是: d={'name':'abc','location':'BeiJing'} if d.has_key('location'): print(d['location']) 在py3中,判断key是否属于字典的写法可以是: d={'name':'abc','location':'BeiJing'} if 'location' in d: print(d['location']) 补充知识:快速解决出现clas
-
python报错: 'list' object has no attribute 'shape'的解决
numpy.array可使用 shape.list不能使用shape. 可以使用np.array(list A)进行转换. (array转list:array B B.tolist()即可) 补充知识:Pandas使用DataFrame出现错误:AttributeError: 'list' object has no attribute 'astype' 在使用Pandas的DataFrame时出现了错误:AttributeError: 'list' object has no attribut
-
解决运行django程序出错问题 'str'object has no attribute'_meta'
如下所示: 报错原因是传入的是类对象,可你传进的参数是字符串,找到传参的位置改过来即可 补充知识:'dict' object has no attribute 'has_key' 解决办法 最近开始学习Python,安装上最新的Python3.6.5 在使用django的时候 出现如下错误 'dict' object has no attribute 'has_key' 保留犯罪现场: 犯罪现场2: 查阅资料发现,Python3以后删除了has_key()方法 解决办法: 修改代码 if dic
-
Pycharm报错:'NoneType' object has no attribute 'bytes'的解决方法
目录 问题描述: 解决方案: 附降级命令: 总结 问题描述: 使用指令 python -m pip install --upgrade pip 升级pip时,Pycharm报错:AttributeError: ‘NoneType’ object has no attribute ‘bytes’如图: 解决方案: 输入指令:easy_install -U pip 如图: 附降级命令: python -m pip install --upgrade pip==9.0.3 总结 到此这篇关于Pycha
-
Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
Python 出现错误TypeError: 'NoneType' object is not iterable解决办法 TypeError: 'NoneType' object is not iterable 这个错误提示一般发生在将None赋给多个值时. def myprocess(): a == b if a != b: return True, value; flag, val = myprocess() 在判断语句中,当if条件不满足,并且没有else语句时,函数默认返回None. 在
随机推荐
- 详解JAVA中的for-each循环与迭代
- iOS swift实现转场动画的方法示例
- php smarty截取中文字符乱码问题?gb2312/utf-8
- 10个php函数实用却不常见
- 零基础学Python(一)Python环境安装
- linux Xtrabackup安装及使用方法
- mysql 导出select语句结果到excel文件遇到问题及解决方法
- Chcp 显示活动控制台代码页数量,或更改该控制台的活动控制台代码页
- 数值运算shell脚本
- sqlserver 存储过程中If Else的用法实例
- Mysql存储引擎特性总结
- PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)
- jQuery实现点击按钮文字变成input框点击保存变成文字
- jQuery使用CSS()方法给指定元素同时设置多个样式
- win2003 远程桌面端口修改方法(注册表)
- C语言 数据结构链表的实例(十九种操作)
- 详解Android的.aar文件生成方法以及使用技巧
- SpringBoot手动使用EhCache的方法示例
- Vim如何使用相对行号实现一切操作详解
- python读取Excel实例详解