NLTK python 错误: “ TypeError: ‘ dict_keys’object is not subscriptable”

我正在按照课堂家庭作业的指示做,我应该在一个文本文件中查找前200个最常用的单词。

这是代码的最后一部分:

fdist1 = FreqDist(NSmyText)
vocab=fdist1.keys()
vocab[:200]

但当我在单词200行后面按回车键时,它会返回:

 Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object is not subscriptable

对于如何修复这个问题,以便它能够正确地返回答案,有什么建议吗?

149374 次浏览

Looks like you are using Python 3. In Python 3 dict.keys() returns an iterable but not indexable object. The most simple (but not so efficient) solution would be:

vocab = list(fdist1.keys())[:200]

In some situations it is desirable to continue working with an iterator object instead of a list. This can be done with itertools.islice():

import itertools
vocal_iterator = itertools.islice(fdist1.keys(), 200)

To print the most frequently used 200 words use: fdist1.most_common(200) The above line of code will return the 200 most frequently used words as key-frequency pair.

If your using python 3 try:

fdist1.most_common(200)

instead, to get the 200 most frequent words.

I am using python 3.5 and I meet the same problem of TypeError.

Using vocab = list(fdist1.keys()) does not give me the top 50 most frequently used words.
But fdist1.most_common(50) does.

Further,if you just want to show those top 50 words not with their frequency,you can try :

[word for (word, freq) in fdist1.most_common(50)]

fdist1 = FreqDist(NSmyText)

vocab=fdist1.keys()

This code is using in Python2.7. So you should do some change. dic.keys() returns an iteratable. So using:

list(fdist1.keys())

If you want to get elements as keys and values (word and frequency), you can use:

list(fdist1.items())[:200]