字频统计1-字符串中没有标点符号

sentence="知之为知之不知为不知"
dict1={}
for s in sentence:
    dict1[s]=dict1.setdefault(s,0)+1
print(dict1)

其中,setdefault(s,0)的作用是:如果dict1中存在键s时,就返回键s的值(某个字的频率);如果dict1中不存在键s时,就将键s的值设置为0。

 

posted on 2018-10-30 09:24  Python大数据  阅读(294)  评论(0编辑  收藏  举报

导航