从容发卡

文章
9
资源
0
加入时间
3年2月3天

Python快速实现词频计算

from collections import Counterfrom itertools import chaindef count_key_value(corpus): # word_freq 是个字典,key=词,value=词频 # Counter 是实现的 dict 的一个子类,可以用来方便地计数,统计词频 # chain函数来自于itertools库,itertools库提供了非常有用的基于迭代对象的函数,而chain函数则是可以串联多个迭代对象来形成一个更大的迭代