计算txt文档词频(字典操作)

计算txt文档词频(字典操作)

# -*- coding: utf-8 -*-

def wordcount(filepath):
    # 打开数据导入
    text = open(filepath, "r", encoding="utf-8")
    words = []
    line = text.readline()
    while line:
        words += line.split()  # 将字符串分割为list,默认分隔符为空格
        line = text.readline()
    text.close()
    
    dic = dict()  # 创建新字典
    # 遍历链表构建字典
    for w in words:
        if len(w) > length:
            length = len(w)
        dic[w] = dic.get(w, 0) + 1
    items = list(dic.items())
    
    # 对列表进行双重排序,先按字典排序,再按频率排序,这样就可以由高到低输出词频
    items.sort()  # 进行字典排序主要为了让具有相同词频的单词仍按照字符顺序排序
    items.sort(key=byFreq, reverse=True)  # 提供关键字reverse并设置为True,可以让python以相反的顺序对列表进行排序
    
    n = int(input("Output analysis of how many words?"))
    for i in range(n):
        words, dic = i
        print("{0:<15}{1:>5} ".format(words, dic)  # 对齐操作:单词在15个空格中左对齐印刷,接着5个空格中是右对齐的数字

def byFreq(pair):  # 为了比较词频:使用键函数,用一对数据作为一个参数,并返回对于数据中的第二项
    return pair[1]

if __name__ == '__main__':
    wordcount("路径")

参考资料:《python程序设计(第3版)》——11.7无顺序集合

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值