1. 程式人生 > >'dict_values' object does not support indexing, Python字典dict中由value查key

'dict_values' object does not support indexing, Python字典dict中由value查key

ima 通過 pre rap error mod returns RR rds

Python字典dict中由value查key

眾所周知,字典dict最大的好處就是查找或插入的速度極快,並且不想列表list一樣,隨著key的增加越來越復雜。但是dict需要占用較大的內存空間,換句話說,字典dict是以空間換速度。詳細請見如下示例:

#------------------------------------------------------------------------------------

student = {‘小萌‘: ‘1001‘, ‘小智‘: ‘1002‘, ‘小強‘: ‘1003‘, ‘小明‘: ‘1004‘}

#------------------------------------------------------------------------------------

由key查value很簡單,直接調用:dict [‘key‘],如下:

#-----------------------------------------------------------------------------------

>>> student [‘小強‘]

結果顯示:‘1003‘

#-----------------------------------------------------------------------------------

但如果此時,我們想由value查找key,則會相對復雜一點,一般來說可通過如下3種方式實現:

#-----------------------------------------------------------------------------------

A. 充分利用 keys() 、values()、index() 函數

>>> list (student.keys()) [list (student.values()).index (‘1004‘)]

結果顯示: ‘小明‘

#-----------------------------------------------------------------------------------

B. 通過定義get_key函數的方式

>>> def get_key (dict, value):

return [k for k, v in dict.items() if v == value]

>>> get_key (student, ‘1002‘)

結果顯示:‘小智‘

#-----------------------------------------------------------------------------------

C. 將原字典dict進行反轉得新字典new_dict,由原來的K-V存儲形式,變為V-K存儲形式

>>> new_dict = {v : k for k, v in dict.items()}

>>> new_dict [‘1001‘]

結果顯示:‘小萌‘

#-----------------------------------------------------------------------------------

雖然我們可以通過以上方式獲得由value查找key的目的,但是我們必須明確一點:在字典dict中,key值是唯一的,且不可變;而value可以隨意取值,且不唯一。之所以強調這一點,是因為在利用上述方法時,會出現失效的情況。如下所示:

假設此時原字典student發生了一些變化,變為:

#-----------------------------------------------------------------------------------

student = {‘小萌‘: ‘1001‘, ‘小智‘: ‘1002‘, ‘小強‘: ‘1003‘, ‘小明‘: [‘1004‘, ‘1005‘]}

#-----------------------------------------------------------------------------------

那麽再次調用上述3種方法,由value查key時,則出現:

#-----------------------------------------------------------------------------------

>>> list (student.keys()) [list (student.values()).index (‘1004‘)]

結果顯示:ValueError: ‘1004‘ is not in list

因為value不唯一,key—‘小明‘ 對應了兩個value,且他們以list形式存儲著,所以如果只取其中一個value值是無法查找對應的key值,必須將多個value值組成的list視為一個整體,即:

>>> list (student.keys()) [list (student.values()).index ([‘1004‘, ‘1005‘])]

結果顯示:‘小明‘

#-----------------------------------------------------------------------------------

>>> def get_key (dict, value):

return [k for k, v in dict.items() if v == value]

>>> get_key (student, ‘1004‘)

結果顯示:[ ]

>>> get_key (student, [‘1004‘, ‘1005‘])

結果顯示:‘小明‘

#-----------------------------------------------------------------------------------

>>> new_dict = {v : k for k, v in dict.items()}

回車後系統報錯:TypeError: unhashable type: ‘list‘

由於key不可變且唯一,當K-V反轉以後,key—‘小明‘ 對應了兩個value組成的list,反過來就變成了key,即此時由list充當key,因為list是可變動的,所以這在Python中是不允許的。

python中dict根據value找到keyname

ShortestImageName=Cost_list.keys()[Cost_list.values().index(min(Cost_list.values()))]

‘dict_values‘ object does not support indexing

In Python 3, dict.values() (along with dict.keys() and dict.items()) returns a view, rather than a list. See the documentation here. You therefore need to wrap your call to dict.values() in a call to list like so:

v = list(d.values())
{names[i]:v[i] for i in range(len(names))}


The immediate answer is that a dict‘s values method returns a non-indexable object, as the error indicates. You can get around that by passing to to list:

list(word_centroid_map.values())

But really you‘d do better to rewrite your loop like this:

for key, value word_centroid_map.items():
    if value == cluster:
        words.append(key)

Or even better, use a list comprehension:

words = [k for k, v in word_centroid_map.items() if v == cluster]

'dict_values' object does not support indexing, Python字典dict中由value查key