Python3错误 TypeError: 'dict_keys' object is not subscriptable

 

问题:python编码中使用 dict.keys() 时,会报 TypeError: 'dict_keys' object is not subscriptable 的错误

Python3错误 TypeError: 'dict_keys' object is not subscriptable

 

解决:在Python3中需要使用 list,如图

Python3错误 TypeError: 'dict_keys' object is not subscriptable

 

原因:dict_keys(['no surfacing','flippers']),返回的是一个 dict_keys 对象,不再是 list 类型,也不支持 index 索引

所以强制转成 list 类型即可使用