4. 词汇表

【问题描述】

打开一英文文章(保存在一个现有文件f_in.txt中),为该文件生成词汇表(存到另一个文件f_out.txt中),要求词汇表中的单词以字典序由小到大存放(只由连续字母组成,且全为小写字母,不重复)。

【输入形式】

保存英文文章的文件f_in.txt位于当前目录下。(注意:程序中使用open()函数打开文件时,文件位置描述只写文件名和扩展名,比如open(“f_in.txt”,“r”))

f_in.txt

【输出形式】

将生成的词汇表以字典序由小到大输出到当前目录下的文件f_out.txt中,每个单词单独占一行。

【样例输入1】

假设文件f_in.txt内容为:

There are two versions of the international standards for C.

The first version was ratified in 1989 by the American National

Standards Institue (ANSI) C standard committee.It is often

referred as ANSI C or C89. The secand C standard was completed

in 1999. This standard is commonly referred to as C99. C99 is a

milestone in C’s evolution into a viable programming language

for numerical and scientific computing.

【样例输出1】

文件f_out.txt中的词汇表应为:

a
american
and
ansi
are
as
by
c
committee
commonly
completed
computing
cs
evolution
first
for
in
institue
international
into
is
it
language
milestone
national
numerical
of
often
or
programming
ratified
referred
scientific
secand
standard
standards
the
there
this
to
two
version
versions
viable
was
【样例输入2】

假设文件f_in.txt内容为:

There are two versions of the international standards for

【样例输出2】

文件f_out.txt中的词汇表应为:

are

for

international

of

standards

the

there

two

versions

【样例说明】

将f_in.txt中出现的所有由字母组成的单词(出现的单个字母也作为单词)全部转换成小写后,按照字典序输出到文件f_out.txt中(每个单词独占一行,重复出现的只输出一次)。

import csv
stopcharacters = [',', '.', ';', '?', '-', ':', '(', ')', '|',"1","2","3","4",'5','6','7','8','9']

f = open('f_in.txt', 'r', encoding='gbk')
text = f.read()

text = text.lower()
for a in stopcharacters:
    text = text.replace(a, ' ')
text = text.replace("'", '')
words = text.split()
words.sort()
words=set(words)
words_list = list(words)
words_list=sorted(words_list)


with open("f_out.txt","w+")as f:
    for i in words_list:
        f.write("{}{}".format(i,"\n"))

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值