【发布时间】:2015-01-07 18:33:30
【问题描述】:
如果文本文件中的每个单词与数组中的单词匹配,我必须计算它的频率,但我收到此错误 TypeError: unhashable type: '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]
【问题讨论】:
-
我不知道是不是应该这样,但是在你的
new_array列表中有重复的条目...删除了重复的条目:new_array=['CC','CD','DT','EX','FW','IN','JJ','JJR','JJS','LS','MD','NN','NNS','NNP','NNPS','PDT','POS','PRP','RB','RBR','RBS','RP','SYM','TO','UH','VB','VBD','VBZ','WDT','WP','WRB']
标签: python arrays string list replace