在Python2.x和Python3.x中dict.keys()的差异
2017-12-05 11:15
330 查看
dict.keys()
Python 中字典(Dictionary) , keys() 函数以列表返回一个字典所有的键。Python2.x和Python3.x有所不同:
在python2.x中,dict.keys()返回一个列表
eg:
dict={'name':'ming','age':20}
dict.keys() Out[67]: ['name', 'age']
在python3.x中,dict.keys()返回一个dict_keys对象,比起列表,这个对象的行为更像是集合set,而不是列表list。
解决方案:list(dict.keys())
eg:
dict={'name':'ming','age':20}
dict.keys() Out[67]: dict_keys(['name', 'age'])
list(dict.keys()) Out[68]: ['name', 'age']
相关文章推荐
- Python 3.x与2.x 差异笔记
- Python2.X与Python3.X编程代码笔记(1):AttributeError: 'dict' object has no attribute 'iteritems'
- 【python】2.X与3.X版本输入输出差异 print input
- python2.X和3.X版本差异问题
- Python2.x和3.x主要差异总结
- python 3.x 与python 2.x语法细节差异比较
- Python2.x和3.x主要差异总结
- 在python2.x中使用dict类型解决没有python3.x中的nonlcoal的问题
- python 3.x 与 2.x区别
- Python2.x与3.x版本区别
- 选择python2.x 还是 3.x
- python2.x和python3.x中raw_input( )和input( )
- python3.x 和 python2.x关于 urllib的用法
- python2.x中函数在python3.x中的改动方法
- Python2.x与3.x版本区别
- windows中Python2.x和Python3.x之间的切换
- Python/Anaconda-python2.x代码转为python3.x代码
- python2.x代码版本迁移至Python3.x
- Python3基础 dict keys+values 循环打印字典中的所有键和值
- 编写同时兼容Python2.x与Python3.x版本的代码的几个示例