我在Python控制台中重复了NLTK书“Python中的自然语言处理”的例子。 (我使用Pycharm 2.7.2,Windows 7,Python 3.3.2)。我是Python新手,不知道如何修复。我今天从https://github.com/nltk/nltk下载了最新的NLTK,但无论如何。
在第39页上它会抛出一个错误:TypeError:'map'对象不是可订阅的
>>> fdist1 = FreqDist(text1)
>>> fdist1
Out[7]: <FreqDist with 19317 samples and 260819 outcomes>
>>> vocabulary1 = fdist1.keys()
>>> vocabulary1[:50]
Traceback (most recent call last):
File "C:\Python33\lib\site-packages\IPython\core\interactiveshell.py", line 2732, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-9-30d7de8cfb37>", line 1, in <module>
vocabulary1[:50]
TypeError: 'map' object is not subscriptable
预期输出应为:
>>> vocabulary1[:50]
[',', 'the', '.', 'of', 'and', 'a', 'to', ';', 'in', 'that', "'", '-',
'his', 'it', 'I', 's', 'is', 'he', 'with', 'was', 'as', '"', 'all', 'for',
'this', '!', 'at', 'by', 'but', 'not', '--', 'him', 'from', 'be', 'on',
'so', 'whale', 'one', 'you', 'had', 'have', 'there', 'But', 'or', 'were',
'now', 'which', '?', 'me', 'like']
答案 0 :(得分:0)
接口可能已更改,您需要执行list(vocabulary1)[:50]
或类似的操作。
见help(map)
>>> a=lambda x: x+2
>>> b = map(a, [1,2,3])
>>> b[0:2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'map' object is not subscriptable
>>> list(b)
[3, 4, 5]
答案 1 :(得分:0)
我遇到了同样的问题。使用vocabulary1 = list(fdist1)
。然后,您可以按vocabulary1[:50]
访问50个最常用的字词。