python3 统计一个list的词频【附代码】

今天爱分享给大家带来 python3 统计一个list的词频【附代码】,希望能帮助到大家。

做统计机器学习经常需要统计,这里我来分享一下怎样统计一个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)]
常见问题
不显示资源下载框怎么办?
请更换谷歌,微软,火狐等浏览器
微云登录不了,或者重复提示登录?
请更换浏览器,最好使用微软,谷歌,火狐。国产浏览器不好用
原文链接:https://blog.itblood.com/1590.html,转载请注明出处。
0

站点公告

显示验证码
没有账号?注册  忘记密码?