使用def function和defaultdict计算特定单词



我下面的代码有问题。我试图创建一个函数,将创建一个标题与指定的单词计数(见下文);但是我总是得到一个错误&;unhashable type: 'list'&;

rlist是一个LIST,但我应该如何使用defaultdict方法?我的*参数是"the" and" and"

''' 
create a function that will 
create a count of headlines with specified words 
rlist: the list of headlines in the list of lists format
args: words to search for
return: a dictionary of each specified word and its count
'''
def wordcount(rlist, *args):
word_count = defaultdict(int)
for headline in rlist:
word_count[headline] += 1

return word_count(args)
print(wordcount(y2006_headline_lists, "the", "and", "cat"))

假设标题是单个单词字符串的列表,这应该能够解决您的问题。您只需要遍历标题并更新word_count。然后,如果您只对返回参数的计数感兴趣,请使用字典推导式过滤掉其余部分。

from collections import defaultdict
y2006_headline_lists = [
["the", "soldier", "returned"],
["I", "and", "the", "cat"],
["cat", "in", "the", "hat"],
]

def wordcount(rlist, *args):
word_count = defaultdict(int)
for headlines in rlist:
for headline in headlines:
word_count[headline] += 1
word_count = {arg: word_count[arg] for arg in args}
return word_count

print(wordcount(y2006_headline_lists, "the", "and", "cat"))

最新更新