做统计机器学习经常需要统计,这里我来分享一下怎样统计一个list里面每个字符串的频率:
from collections import Counter
word_count=['I','am','from','China',',','I','Love','my','country','!','!','!']
counter = Counter(word_count)
dictionary=dict(counter)
# get to k most frequently occuring words
k=10
res=counter.most_common(k)
print(res)
输出为:
[('!', 3), ('I', 2), ('am', 1), ('from', 1), ('China', 1), (',', 1), ('Love', 1), ('my', 1), ('country', 1)]