您的位置:首页 > 编程语言 > Python开发

python统计单词频率基本算法。

2018-02-23 00:30 369 查看
counts=dict()
names=['aaa','bbb','bbb','ccc','aaa']
print(counts);
for name in names:
if name not in counts:
counts[name] = 1
else:
counts[name] = counts[name]+1
print(counts)
1. counts先设置为空字典
2. name里的每一个元素,若(if)不在空字典就每个值设定为1
3. 如果又出现了一次(else),则在步骤2的基础上增加一次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 算法