[英]Calculate the frequency of words in python
I have to calculate the frequency of each word which is there in text file if it matches with the word which is there in an array but i am getting this error TypeError: unhashable type: 'list' 如果它与数组中存在的单词匹配,我必须计算文本文件中存在的每个单词的出现频率,但是我收到此错误TypeError:无法散列的类型:'list'
import string
from collections import Counter
from array import *
cnt=Counter()
word =[ ]
word_count = [ ]
new_array =['CC','CD','DT','EX','FW','IN','JJ','JJR','JJS','LS','MD','NN','NNS','NNP','NNPS','PDT',
'POS','PRP','PRP','RB','RBR','RBS','RP','SYM','TO','UH','VB','VBD','VBZ','WDT','WP','WP','WRB']
file = open('output.txt', 'rU')
for line in file:
new_line = line.replace("_"," ")
words = new_line.split()
word.append(words)
[(w, word.count(w)) for w in set(word) if w in new_array]
When you do word.append(words)
, you are appending a list to a list, there by making a list of lists. 当您执行word.append(words)
,您将通过在列表列表中添加列表到列表中。 As a list is not hashable, a list of lists cannot be converted into a set and you were getting that error. 由于列表不可散列,因此列表列表无法转换为集合,因此您将收到该错误。
I think you were intending to do word += words
instead. 我认为您打算改用word += words
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.