Python 词频统计 并以字典形式输出

对给出的列表进行词频统计,将单词与单词出现次数对应起来,以字典的形式输出

列表如下

l1=['a','a','b','b','b','c','c','c','c','d','d','d','d']

l1=['a','a','b','b','b','c','c','c','c','d','d','d','d']
print(l1)
b=dict.fromkeys(l1,0)
print(b)
for x in l1:
    b[x]+=1
print(b)

输出结果:

['a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd']
{'a': 0, 'b': 0, 'c': 0, 'd': 0}
{'a': 2, 'b': 3, 'c': 4, 'd': 4}

 

你可能感兴趣的:(Python 词频统计 并以字典形式输出)