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()