Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类型错误:';地图';对象不可下标在Python 3中出现错误_Python_Nltk_Typeerror - Fatal编程技术网

类型错误:';地图';对象不可下标在Python 3中出现错误

类型错误:';地图';对象不可下标在Python 3中出现错误,python,nltk,typeerror,Python,Nltk,Typeerror,我正在尝试使用FreqDist,它是Python中NLTK的一部分。 我尝试了以下示例代码: fdist1 = FreqDist(text1) vocabulary1 = fdist1.keys() vocabulary1[:50] 但最后一行给了我这个错误: TypeError: 'map' object is not subscriptable 我认为该代码在Python2上运行良好,但在Python3(我拥有的)上会出现上述错误 为什么会出现此错误以及如何解决?非常感谢您在这方面的帮助

我正在尝试使用FreqDist,它是Python中NLTK的一部分。 我尝试了以下示例代码:

fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
vocabulary1[:50]
但最后一行给了我这个错误:

TypeError: 'map' object is not subscriptable
我认为该代码在Python2上运行良好,但在Python3(我拥有的)上会出现上述错误


为什么会出现此错误以及如何解决?非常感谢您在这方面的帮助。

您必须首先将其转换为列表:

new_vocab= list(vocabulary1)
...= new_vocab[:50]
在Python3中,
.keys()
返回一个迭代器,不能对其进行切片。在切片之前将其转换为列表

fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
x = list(vocabulary1)[:50]
# or...
vocabulary1 = list(fdist1.keys())
x = vocabulary1[:50]

在调用fdist1.keys()时添加
list()
哈哈,就像OP一样,我在阅读Oreilly关于NLTK的教科书时也被难住了!我想我现在必须习惯Python 3了。。