001、python报错
>>> dict1 = {"aa":300, "bb":500, "cc":400, "dd":700} >>> dict1 {'aa': 300, 'bb': 500, 'cc': 400, 'dd': 700} >>> dict1.keys() dict_keys(['aa', 'bb', 'cc', 'dd']) >>> dict1.keys()[3] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'dict_keys' object is not subscriptable
002、报错原因 :python3不支持该语法
003、解决方法
>>> dict1 = {"aa":300, "bb":500, "cc":400, "dd":700} >>> dict1 {'aa': 300, 'bb': 500, 'cc': 400, 'dd': 700} >>> dict1.keys() dict_keys(['aa', 'bb', 'cc', 'dd']) >>> dict1.keys()[2] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'dict_keys' object is not subscriptable >>> list(dict1.keys())[2] ## 增加list函数 'cc'
标签:aa,TypeError,dict1,bb,keys,cc,dict From: https://www.cnblogs.com/liujiaxin2018/p/17451502.html