' Dict_values ' object does not support indexing, Python dictionary dict by value in key

Source: Internet
Author: User
Tags ming

Python dictionary dict by value in key

As we all know, the biggest advantage of dictionary dict is that it is very fast to find or insert, and does not want to list lists, as key increases more and more complex. But Dict need to occupy a large amount of memory space, in other words, the dictionary dict is to change the speed of space. For details, see the following example:

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

Student = {' Xiao Meng ': ' 1001 ', ' Little Wisdom ': ' 1002 ', ' Xiao Qiang ': ' 1003 ', ' xiaoming ': ' 1004 '}

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

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

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

>>> student [' Xiao Qiang ']

Results show: ' 1003 '

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

But if we want to find the key by value at this point, it will be a bit more complex, generally in the following 3 ways:

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

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

>>> list (Student.keys ()) [List (Student.values ()). Index (' 1004 ')]

Results show: ' 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 ')

Results show: ' Xiao Zhi '

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

C. Dict the original dictionary to the new Dictionary new_dict, from the original K-v storage form, into the V-k storage form

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

>>> new_dict [' 1001 ']

The results show: ' Xiao Meng '

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

Although we can obtain the purpose of finding the key by value in the above way, we must be clear: in dictionary dict, the key value is unique and immutable, and value can be arbitrarily valued and not unique. This is emphasized because of the failure to use the above method. As shown below:

Assuming that the original dictionary student has undergone some changes, it becomes:

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

Student = {' Little moe ': ' 1001 ', ' Little Wisdom ': ' 1002 ', ' Xiao Qiang ': ' 1003 ', ' xiaoming ': [' 1004 ', ' 1005 ']}

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

Then the above 3 methods are called again, when the value is checked for key, it appears:

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

>>> list (Student.keys ()) [List (Student.values ()). Index (' 1004 ')]

Results show: valueerror: ' 1004 ' is not in list

Because value is not unique, key-' xiaoming ' corresponds to two value and they are stored in list form, so if only one of the value values is not able to find the corresponding key value, a list of multiple value values must be considered as a whole, namely:

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

Results show: ' 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: []

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

Results show: ' Xiao Ming '

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

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

System error after carriage return: typeerror:unhashable type: ' List '

Since key is immutable and unique, when K-V is reversed, key-' xiaoming ' corresponds to a list of two value, which in turn becomes key, i.e. the list acts as a key at this point, because the list is variable, so this is not allowed in Python. In Python, Dict finds Keynameshortestimagename=cost_list.keys () [Cost_list.values () based on value. 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-wrap your call-to-a-like so dict.values() list :

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


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

list(word_centroid_map.values())

But really you ' d does 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 dictionary dict by value in key

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.