Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLTK python error: "TypeError: 'dict_keys' object is not subscriptable"

I am following instructions for a class homework assignment and I am supposed to look up the top 200 most frequently used words in a text file.

Here's the last part of the code:

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

But when I press enter after the vocab 200 line, it returns:

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

Any suggestions on how to fix this so it can correctly return an answer?

like image 488
user3760644 Avatar asked Oct 16 '14 01:10

user3760644


2 Answers

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)
like image 192
Klaus D. Avatar answered Oct 08 '22 13:10

Klaus D.


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)]

like image 23
Roy Chen Avatar answered Oct 08 '22 14:10

Roy Chen