python:TypeError: ‘dict_keys‘ object does not support indexing

 1. 报错:python:TypeError: 'dict_keys' object does not support indexing

 

 

2. Solve:

https://blog.csdn.net/a070220106/article/details/9089379  TypeError: 'dict_keys' object does not support indexing

https://blog.csdn.net/shuiyixin/article/details/87910896   Python error: TypeError:'dict_keys' object does not support indexing (machine learning actual combat treePlotter code) solution

 

Since python3 changed dict.keys, it returns a dict_keys object, which supports iterable but not indexable, we can explicitly convert it into a list:

Replace the following code:

 firstStr = inputTree.keys()[0]

Change to:

 firstStr = list(inputTree.keys())[0]

That's it.

 

Guess you like

Origin blog.csdn.net/weixin_39450145/article/details/113838765