首页 > 其他分享 >AttributeError: 'dict_values' object has no attribute 'index'

AttributeError: 'dict_values' object has no attribute 'index'

时间:2023-06-02 13:11:23浏览次数:39  
标签:index dict1 no 300 attribute values

 

001、python报错如下:

>>> dict1 = {"aa":700, "bb":400, "cc":300, "dd":600}
>>> dict1.values().index(300)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict_values' object has no attribute 'index'

 

002、报错原因,python3不支持 index用法

 

003、解决方法

>>> dict1 = {"aa":700, "bb":400, "cc":300, "dd":600}
>>> dict1.values().index(300)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict_values' object has no attribute 'index'
>>> list(dict1.values()).index(300)      ## 增加list函数
2

 

标签:index,dict1,no,300,attribute,values
From: https://www.cnblogs.com/liujiaxin2018/p/17451489.html

相关文章