Python 实验六作业 词频数统计

1.请统计hamlet.txt文件中出现的英文单词情况,统计并输出出现最多的前n个单词,注意:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

(1) 单词不区分大小写,即需将大写转换成小写;‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

(2) 请在文本中剔除如下特殊符号:!"#$%&()*+,-./:;<=>?@[\]^_‘{|}~‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

(3) 输出10个单词和其出现次数,每个单词一行;‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

(4) 输出单词为小写形式。

此题不涉及编码转换,若想指定编码 可在开始加上

#-- coding: utf-8 --

或 在文件打开处 指定编码

with open(“hamlet.txt”, “r”, encoding=‘utf-8’) as f:

 ........

 ........

【输入形式】

【输出形式】

以下仅是输出样例(仅列出3个,需要列出n个),不是最终结果:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

the 1138

and 965

to 754

单词 左对齐,并占10个位置; 次数 右对齐,并占5个位置

n=int(input())
f=open("hamlet.txt","r")
txt=f.read()
txt=txt.lower()
for i in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
    txt=txt.replace(i," ")
words=txt.split()
counts={}
for word in words:
    counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(n):
    word,count=items[i]
    print("{:<10}{:>5}".format(word,count))
f.close()

2.【问题描述】

使用freqDict = eval(input()) 读入单词词频字典,再读入一段英文,默认按照英文输入的顺序,统计更新单词词频字典,并输出。
【输入形式】

输入为两行,第一行是一个字典,形如{‘hello’: 12, ‘world’: 10},其中存储初始的词频数据。第二行是一段英文文本。
【输出形式】

输出一行,直接打印输出更新后的字典。
【样例输入】

{}

hello world
【样例输出】

{‘hello’: 1, ‘world’: 1}

freqdict=eval(input())
x=input().split()
for word in x:
    freqdict[word]=freqdict.get(word,0)+1
print(freqdict)

3.【问题描述】

文件src.txt存储的是一篇英文文章,将其中所有大写字母转换成小写字母存入文件dest.txt。

【样例输入】

src.txt里面存储内容为: This is a Book

【样例输出】

生成文件dest.txt。内容为: this is a book

fr=open("src.txt","r")
fw=open("dest.txt","w")
txt=fr.read()
txt=txt.lower()
fw.write(txt)
fr.close()
fw.close()
  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值