Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误:';dict#u值';对象不支持索引_Python_Python 3.x - Fatal编程技术网

Python 错误:';dict#u值';对象不支持索引

Python 错误:';dict#u值';对象不支持索引,python,python-3.x,Python,Python 3.x,我正在尝试使用Kaggle Bag of Words模块,在某一点上,它抛出了以下错误: kmeans_clustering = KMeans( n_clusters = num_clusters ) idx = kmeans_clustering.fit_predict( word_vectors ) word_centroid_map = dict(zip( model.wv.index2word, idx )) 现在, 错误是: -----------------------------

我正在尝试使用Kaggle Bag of Words模块,在某一点上,它抛出了以下错误:

kmeans_clustering = KMeans( n_clusters = num_clusters )
idx = kmeans_clustering.fit_predict( word_vectors )
word_centroid_map = dict(zip( model.wv.index2word, idx ))
现在,

错误是:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-f230ff75f988> in <module>()
      3         words = []
      4         for i in range(0,len(word_centroid_map.values())):
----> 5             if( word_centroid_map.values()[i] == cluster ):
      6                 words.append(word_centroid_map.keys()[i])
      7         print(words)

TypeError: 'dict_values' object does not support indexing
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
3字=[]
4表示范围内的i(0,len(word\u centroid\u map.values()):
---->5如果(单词质心映射值()[i]==簇):
6个单词。追加(单词\u形心\u映射.keys()[i])
7印刷(字)
TypeError:“dict_values”对象不支持索引

直接的答案是dict的
values
方法返回一个不可索引的对象,如错误所示。您可以通过传递到列表来解决此问题:

list(word_centroid_map.values())
但实际上,您最好像这样重写您的循环:

for key, value word_centroid_map.items():
    if value == cluster:
        words.append(key)
或者更好的是,使用列表理解:

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

word\u centroid\u map.values()
返回无法索引的dict\u values对象

但是,如果要为它编制索引,可以通过执行
列表(word\u centroid\u map.values())
来转换它

这里有一个非常简单的例子来说明我的意思:

>>> d = {'a': [1,2,3,4], 'b':[5,6,7,8]}
>>> d.values()
dict_values([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> d.values()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_values' object does not support indexing
>>> list(d.values())[0]
[1, 2, 3, 4]
>d={'a':[1,2,3,4],'b':[5,6,7,8]}
>>>d.价值观()
dict_值([[1,2,3,4],[5,6,7,8]]
>>>d.值()[0]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:“dict_values”对象不支持索引
>>>列表(d.values())[0]
[1, 2, 3, 4]
>>> d = {'a': [1,2,3,4], 'b':[5,6,7,8]}
>>> d.values()
dict_values([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> d.values()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_values' object does not support indexing
>>> list(d.values())[0]
[1, 2, 3, 4]