'dict_values' object does not support indexing, the key is checked by value in the Python dictionary dict

 

Find key by value in Python dictionary dict

As we all know, the biggest advantage of the dictionary dict is that it is extremely fast to look up or insert, and it is not like the list list, which becomes more and more complex as the key increases. But dict needs to occupy a large memory space, in other words, the dictionary dict is space for speed. For details, see the following example:

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

student = {'Xiaomeng': '1001', 'Xiaozhi': '1002', 'Xiaoqiang': '1003', 'Xiaoming': '1004'}

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

It is very simple to check the value by key, call directly: dict ['key'], as follows:

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

>>> student ['Xiaoqiang']

The result shows: '1003'

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

But if at this time, we want to find the key by value, it will be relatively complicated. Generally speaking, it can be achieved in the following three ways:

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

A. Make full use of the keys(), values(), index() functions

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

The result shows: 'Xiao Ming'

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

B. By defining the get_key function

>>> def get_key (dict, value):

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

>>> get_key (student, '1002')

Result: 'Xiaozhi'

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

C. Reverse the original dictionary dict to get a new dictionary new_dict, which changes from the original KV storage form to the VK storage form

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

>>> new_dict ['1001']

The result shows: 'Xiaomeng'

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

Although we can obtain the purpose of finding the key by value through the above methods, we must be clear: in the dictionary dict, the key value is unique and immutable; and the value can take any value and is not unique. This is emphasized because failures can occur when using the above methods. As follows:

Suppose that the original dictionary student has undergone some changes at this time, becoming:

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

student = {'Xiaomeng': '1001', 'Xiaozhi': '1002', 'Xiaoqiang': '1003', 'Xiaoming': ['1004', '1005']}

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

Then call the above three methods again, and when the key is checked by value, it appears:

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

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

The result shows: ValueError: '1004' is not in list

Because the value is not unique, the key—'Xiao Ming' corresponds to two values, and they are stored in the form of a list, so if you only take one of the value values, you cannot find the corresponding key value, and you must view the list composed of multiple value values. as a whole, namely:

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

The result shows: 'Xiao Ming'

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

 

>>> def get_key (dict, value):

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

>>> get_key (student, '1004')

The results show that:[ ]

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

The result shows: 'Xiao Ming'

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

 

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

After pressing Enter, the system reports an error: TypeError: unhashable type: 'list'

Since the key is immutable and unique, when the KV is reversed, the key—'Xiao Ming' corresponds to the list composed of two values, which in turn becomes the key, that is, the list acts as the key at this time, because the list is changeable, So this is not allowed in Python.
 
 
 

In python, dict finds keyname according to value

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]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324679445&siteId=291194637