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

后端 未结 5 1094
鱼传尺愫
鱼传尺愫 2020-12-01 03:36

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 par

相关标签:
5条回答
  • 2020-12-01 03:51

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

    0 讨论(0)
  • 2020-12-01 03:56

    If your using python 3 try:

    fdist1.most_common(200)
    

    instead, to get the 200 most frequent words.

    0 讨论(0)
  • 2020-12-01 03:59

    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.

    0 讨论(0)
  • 2020-12-01 04:00

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

    0 讨论(0)
  • 2020-12-01 04:02

    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())
    
    0 讨论(0)
提交回复
热议问题