词频统计

#count_hamlet.py
# -*- coding: utf-8 -*-
def get_txt(filename):
    #获取文件内容,并将其中的标点符号以空格替换,返回文本内容
    with open(filename,"r") as f:
        txt = f.read()
        txt = txt.lower()
        for ch in "!^~@#$%&()-_+-*\=[]{}|;:'<>,./?" :
            txt = txt.replace(ch ,"")
        #内容要缩进,不需要f.close()
    return txt

hamlet_txt = get_txt("D:\python_work\mytxt\hamlet.txt") 
#获取单词列表,待排除的单词{集合}
words = hamlet_txt.split()
excludes = {'i','you','he','she','we','my','your','his','her','our',
    'they','their','me','him','them','it','its','this','that',
    'be','been','is','are','was','were','no','not',
    'the','a','an','there','here',
    'in','on','of','for','with','to','as','so',
    'will','can','shall','may','would','could','should','might',
    'must','need','ought','have','had',
    'and','but','or',
    'what','when','who','where','which','hamlet'}

#创建字典,以单词为键,以频数为键值
counts = {}
for word in words:
    if word in excludes:
        continue
    counts[word] = counts.get(word,0) +1
#将字典转换为列表,其元素为元组
items = list(counts.items())

#采用匿名函数,以频数为关键字进行从高到低排序
items.sort(key = lambda x : x[1],reverse = True)
#打印前三十个频数最高的单词
for i in range(30):
    word,count = items[i]
    print("{0:<10s}{1:>4d}".format(word,count))

这里要注意:
1.创建集合的方式 {::::::::::::::},或者用set()创建空集合,set(list)函数可以将列表转换为集合;
2.list()函数
3.列表的sort()函数,降序:reverse= True
4.lambda()函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值