完美解决丨+# TypeError: ‘dict_keys‘ object does not support indexing

简介: 完美解决丨+# TypeError: ‘dict_keys‘ object does not support indexing

+ + 结构 + +``` +- 标题 +- 问题描述 +- 代码栗子 +- 总结 +``` + + 目录 + + TypeError: 'dict_keys' object does not support indexing + + 如何实现? + +```python +a = {'a': 1} +b = a.keys() +c = b[0] +``` + + 异常描述 + +``` +TypeError Traceback (most recent call last) +<ipython-input-9-9dceb06f3f84 in <module() + 2 a = {'a': 1} + 3 b = a.keys() +---- 4 c = b[0] + +TypeError: 'dict_keys' object does not support indexing +``` + + 原因说明 + +错误原因在于,字典的 keys 方法返回的是 dict_keys 对象,而这种对象不支持 indexing 。 + + 如何解决? + + 将 dict_keys 转换成 list + +只要将返回的 dict_keys 对象转换成 list 即可。 + +```python +a = {'a': 1} +b = list(a.keys()) +c = b[0] +``` + + 使用迭代 + +```python +a = {'a': 1} +for key in a: + print(key) +``` + + 参考 + +- TypeError: 'dict_keys' object does not support indexing


相关文章
Vue3接口数据报错TypeError: target must be an object
Vue3接口数据报错TypeError: target must be an object
1370 0
Python错误 TypeError: ‘NoneType‘ object is not subscriptable解决方案汇总
Python错误 TypeError: ‘NoneType‘ object is not subscriptable解决方案汇总
|
4月前
|
存储 JSON JavaScript
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
225 1
|
6月前
|
JSON 数据格式
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
|
4月前
|
JSON 前端开发 数据格式
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
83 0
|
4月前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
119 0
|
4月前
|
开发者 Python
【Python】已解决:TypeError: a bytes-like object is required, not ‘int’
【Python】已解决:TypeError: a bytes-like object is required, not ‘int’
199 0
|
6月前
|
JSON 数据格式 Python
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
177 1
|
机器学习/深度学习 自然语言处理 数据可视化
简单的知识图谱可视化+绘制nx.Graph()时报错TypeError: ‘_AxesStack‘ object is not callable
简单的知识图谱可视化+绘制nx.Graph()时报错TypeError: ‘_AxesStack‘ object is not callable
194 0
|
人工智能 自然语言处理 JavaScript
vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in created hook: “TypeError: Object(...) is not a func
vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in created hook: “TypeError: Object(...) is not a func
118 0

热门文章

最新文章