字典的数据采集模块

来源:互联网 发布:少儿编程前景 编辑:程序博客网 时间:2024/05/16 11:21

collections模块:

from collections import defaultdict  #引入默认字典x = 'you are you are u u many are'# 实现统计句子中的词出现频率dic = defaultdict(int)  #采用int函数作为参数for i in x.split():    dic[i]+=1print dicdefaultdict(<type 'int'>, {'u': 2, 'you': 2, 'are': 3, 'many': 1})from collections import Counterprint Count(x.split())  #计算每个词出现的频率,返回字典{'u': 2, 'you': 2, 'are': 3, 'many': 1}
原创粉丝点击