pylab: TypeError: unhashable type: 'list'

117 views
Skip to first unread message

typetoken

unread,
Aug 26, 2012, 1:21:42 PM8/26/12
to nltk-...@googlegroups.com
For the exercise 23 on page 76, or  http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html
I try the following codes but it pops up "TypeError: unhashable type: 'list"

>>> import nltk
>>> text = nltk.corpus.brown.words(categories = 'news')
>>> def zif(text):
fdist = nltk.FreqDist(text)
import pylab
word = fdist.keys()
x = []
y = []
index = []
for i in range(0,len(set(text)),1):
x.extend(word[i:i+1])
y.extend(fdist[word[i:i+1]])
index.extend('i+1')
pylab.plot(index, y,'b')
pylab.title('zipf law')
pylab.xlabel('word rank')
pylab.ylabel('word frequency')
pylab.show()

>>> zif(text)

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    zif(text)
  File "<pyshell#3>", line 10, in zif
    y.extend(fdist[word[i:i+1]])
  File "C:\Python27\lib\site-packages\nltk\probability.py", line 495, in __getitem__
    return self.get(sample, 0)
TypeError: unhashable type: 'list'

Any suggestions or solutions? thanks indeed.

John H. Li

unread,
Sep 2, 2012, 11:39:10 PM9/2/12
to nltk-...@googlegroups.com
Now the solution has been found: 
For the previous codes, I replace y.extend(fdist[word[i:i+1]]) with y.extend([fdist[word[i]]]). Then it works. I come to realize the difference between y.extend('90') , y.extend(['90']),  y.extend(90) and y.extend([90]).  



--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nltk-users/-/cBglUyK4yEcJ.
To post to this group, send email to nltk-...@googlegroups.com.
To unsubscribe from this group, send email to nltk-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nltk-users?hl=en.

Reply all
Reply to author
Forward
0 new messages