NLTK python错误:"TypeError:'dict_keys'对象不可下标" [英] NLTK python error: "TypeError: 'dict_keys' object is not subscriptable"
本文介绍了NLTK python错误:"TypeError:'dict_keys'对象不可下标"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在按照课堂作业的说明进行操作,应该在文本文件中查找前200个最常用的单词.
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]
但是当我在vocab 200行之后按Enter键时,它将返回:
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?
推荐答案
好像您正在使用Python3.在Python 3中,dict.keys()
返回一个可迭代但不可索引的对象.最简单(但效率不高)的解决方案是:
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())
这篇关于NLTK python错误:"TypeError:'dict_keys'对象不可下标"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文