1

Python コンソールで NLTK の本「Python による自然言語処理」の例を繰り返しています。(私は Pycharm 2.7.2、Windows 7、Python 3.3.2 を使用しています)。私はPythonが初めてで、修正方法がわかりません。今日、 https: //github.com/nltk/nltk から最新の NLTK をダウンロードしましたが、問題ありません。

39 ページでエラーがスローされます: TypeError: 'map' object is not subscriptable

>>> 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']
4

2 に答える 2

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]
于 2013-06-06T18:43:52.560 に答える
0

私も同じ問題を抱えていました。を使用しvocabulary1 = list(fdist1)ます。次に、最も頻繁に使用される 50 の単語にアクセスできますvocabulary1[:50]

于 2013-07-08T16:20:11.243 に答える