如果我有一个频率字典,其中包含文本中的单词作为键,以及它们在文本中作为值出现的次数。如何通过确保考虑多次出现的单词来获得单词计数和平均长度?现在我所拥有的只是创建一个键列表(因为它们是单词),然后只使用len()进行字数统计。
wordcount=len(list(freq.keys()))
report["count:"]=wordcount
#for average length:
avg=list(freq.keys())
average=sum(map(len,avg))/len(avg)
report["avglen"]=average
答案 0 :(得分:0)
使用sum
功能和dict.values()
:
freq = { 'test' : 10, 'rep' : 100 }
wordcount = sum(freq.values())
average = sum(len(w) * c for w, c in freq.items()) / wordcount
print(wordcount, average)
答案 1 :(得分:0)
number_of_words = int(raw_input("Enter the number of words. "))
word_dict = {}
for i in range(number_of_words):
word = raw_input("Enter word. ")
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
print word_dict
print sum([len(word)*word_dict[word] for word in word_dict])/number_of_words
一个非常相似的问题:https://stackoverflow.com/questions/20143947/word-frequency-counter-python/20145320#20145320