Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
作者:bladestone 时间:2023-04-21 05:52:45
引言
目前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的进化需要一个过程,其中的差异可以参照如下文章:https://www.jb51.net/article/105518.htm
来源:http://blog.csdn.net/blueheart20/article/details/54600511
标签:python3,错误解决,AttributeError
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python如何在终端彩色打印输出
2022-03-10 19:56:35
![](https://img.aspxhome.com/file/2023/9/82619_0s.png)
MySQL单表查询进阶教程(最全面!)
2024-01-22 21:06:42
![](https://img.aspxhome.com/file/2023/7/68127_0s.png)
懒就要懒到底——鼠标自动点击(含时间判断)
2024-04-22 22:18:06
Python实现图片转字符画的示例代码
2021-07-13 19:21:03
![](https://img.aspxhome.com/file/2023/6/97506_0s.png)
python中if嵌套命令实例讲解
2022-03-11 21:52:51
python如何修改文件时间属性
2022-12-04 06:38:24
![](https://img.aspxhome.com/file/2023/0/127590_0s.png)
php生成随机密码的三种方法小结
2023-11-22 04:25:50
php计算两个整数的最大公约数常用算法小结
2023-11-20 00:29:01
基于CentOS搭建Python Django环境过程解析
2021-09-10 07:14:58
![](https://img.aspxhome.com/file/2023/7/128467_0s.png)
Python 画出来六维图
2022-03-14 04:01:15
![](https://img.aspxhome.com/file/2023/8/124248_0s.jpg)
Go 结构体序列化的实现
2024-05-21 10:26:40
![](https://img.aspxhome.com/file/2023/9/123969_0s.jpg)
Mysql的列修改成行并显示数据的简单实现
2024-01-24 01:39:54
![](https://img.aspxhome.com/file/2023/0/88500_0s.png)
驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接,错误:“The server selected protocol version TLS10 is not accepted by client
2024-01-14 21:42:28
![](https://img.aspxhome.com/file/2023/2/110942_0s.png)
关于Python中进度条的六个实用技巧分享
2023-07-03 09:58:39
![](https://img.aspxhome.com/file/2023/2/98332_0s.png)
解决py2exe打包后,总是多显示一个DOS黑色窗口的问题
2022-05-22 21:51:51
IE6/7/8/9中Table/Select的innerHTML不能赋值的解决方法
2024-04-10 16:16:48
![](https://img.aspxhome.com/file/2023/0/136740_0s.png)
利用Python脚本实现传递参数的三种方式分享
2023-07-11 03:45:44
![](https://img.aspxhome.com/file/2023/0/83710_0s.png)
python 获取域名到期时间的方法步骤
2022-09-02 13:37:43
详解Python中的字符串格式化
2023-09-10 22:38:14
![](https://img.aspxhome.com/file/2023/9/83879_0s.png)
asp清空站点缓存
2009-08-04 18:01:00