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

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
vocab_iterator = itertools.islice(fdist1.keys(), 200)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)