【问题标题】:Calculate the frequency of words in python计算python中单词的频率
【发布时间】: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


【解决方案1】:

当您执行word.append(words) 时,您是在将一个列表附加到一个列表中,从而制作一个列表列表。 由于列表不可散列,因此无法将列表列表转换为集合,并且您遇到了该错误。

我认为您打算改为使用word += words

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 2023-03-22
    • 2011-07-21
    • 2020-04-06
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多