开发者社区> 问答> 正文

使用defaultdict进行词频统计时该注意什么问题呢?

已解决

使用defaultdict进行词频统计时该注意什么问题呢?

展开
收起
出笼的鸟 2022-07-25 13:56:04 341 0
1 条回答
写回答
取消 提交回答
  • 推荐回答

    词频统计,使用defaultdict

    #使用dict时,如果引用的Key不存在,就会抛出KeyEror。如果希望key不存在时,返回一个默认值,就可以用de

    In [52]: # Count word frequencies
    from collections import defaultdict
    frequency = defaultdict (int)
    for text in texts:
    for token in text:
    frequency[tokenl]I+=1
    In [53]: #Only keep words that appear more than once
    processed corpus = [token for token in text if frequency[token]>1] for text in texts]
    In [62]: processed corpus[1]
    In [54]: len(processed corpus)
    
    2022-07-25 14:41:38
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载