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

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]
上一篇:oracle 11g centos6 安装


下一篇:2021版的思维导图MindManager 安装激活以及换机教程