defaultdict初始化字典,封装起来用来,计数,计算词频
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from icecream import ic from collections import defaultdict texts = [ ["楚枫","楚月","爱"], ["修罗","楚枫"], ["楚月"] ] default_dict = defaultdict(int) for text in texts: for word in text: default_dict[word] += 1 ic(default_dict) """ 这个东西:初始化字典,默认value=0,然后你可以做加减 就相当于封装了你赋值的时候,查出来没有,但是你就赋值为1, 如果有,则在基础上加一 """
此模块省下你很多的时间
-----------------------------------------------------------------------------------------------------------------------------------------