【计算机二级python】综合题目

计算机二级python真题



在这里插入图片描述


题目一:全球大学排名

在省略号处填写一行或多行代码,完成如下功能‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

data. txt,其中记录了2019年QS全球大学排名前20名的学校信息,示例如下:

1,麻省理工学院,美国
2,斯坦福大学,美国
3,哈佛大学,美国

第一列为排名,第2列为学校名称,第3列为学校所属的国家,字段之间用逗号’,'隔开‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

程序读取data.txt文件内容,统计出现的国家个数以及每个国家上榜大学的数量及名称,输出结果格式示例如下:

示例

美国: 11 : 麻省理工学院,斯坦福大学,哈佛大学,加州理工学院,芝加哥大学,普林斯顿大学,康奈尔大学,耶鲁大学,哥伦比亚大学,宾夕法尼亚大学,密歇根大学安娜堡分校
英国: 5 : 牛津大学,剑桥大学,帝国理工学院,伦敦大学学院,爱丁堡大学

f = open('data.txt', 'r',encoding="utf-8")

unis={"美国":[],"英国":[],"瑞士":[],"新加坡":[],"中国":[]}

for line in f:
    if line == "\n":
        continue
    else:
        txt = line.split(",")
        txt[2] = txt[2].replace('\n',"")
        unis[txt[2]].append(txt[1])
f.close()    

for d in unis:
    print('{:>4}: {:>4} : {}'.format(d,len(unis[d]),",".join(unis[d])))

题目二:红楼梦

文本文件“红楼梦. txt”中包含了《红楼梦》小说前20章内容,“ 停用词. txt”包含了需要排除的词语。请修改模板,实现以下功能。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

1.对“红楼梦. txt”中文本进行分词,并对人物名称进行归-化处理,仅归一化以下内容:‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

  • 凤姐、凤姐儿、凤丫头归-为凤姐
  • 宝玉、二爷、宝二爷归-为宝玉
  • 黛玉、颦儿、林妹妹、黛玉道归-为黛玉
  • 宝钗、宝丫头归一为宝钗
  • 贾母、老祖宗归-为贾母
  • 袭人、袭人道归一为袭人
  • 贾政、贾政道归一为贾政
  • 贾链、琏二爷归一为贾琏

2.不统计“停用词.txt"文件中包含词语的词频(名字必须大于一个字)。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

3.提取出场次数不少于40次的人物名称,将人物名称及其出场次教按照递减排序,保存到result.csv文件中,出场次数相同的.则按照人物名称的字符顺序排序。‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

输出示例‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

宝玉,597
凤姐,296
一个,179
如今,132
黛玉,113
一面,112

import jieba

f = "红楼梦.txt"
sf = "停用词.txt"

txt = jieba.lcut(open(f,"r",encoding="utf-8").read())


stop_words = []

f = open(sf,"r",encoding="utf-8")
for i in f.read().splitlines():  # 读取停用词文本并分割文本后添加到stop_words列表中
    stop_words.append(i)

# 剔除停用词    
txt0 = [x for x in txt if x not in stop_words]

# 统计词频
counts={}

for word in txt0:
    if len(word) <= 1:   # 跳过标点符号和字
        continue
    elif word == "凤姐儿" or word == "凤丫头":
        rword = "凤姐"
    elif word == "二爷" or word == "宝二爷":
        rword = "宝玉"
    elif word == "颦儿" or word == "林妹妹" or word == "黛玉道":
        rword = "黛玉"
    elif word == "宝丫头":
        rword = "宝钗"
    elif word == "老祖宗":
        rword = "贾母"
    elif word == "袭人道":
        rword = "袭人"
    elif word == "贾政道":
        rword = "贾政"
    elif word == "琏二爷":
        rword = "贾琏"
    else:
        rword = word
    counts[rword]=counts.get(rword,0)+1

# 固定语句将字典的值进行排序
li = list(counts.items())
li.sort(key=lambda x:x[1], reverse=True)


# 列出词频超过40的结果
f = open(r"result.csv","a",encoding="gbk")  
for i in li:
    key,value = i
    if value < 40:
        break
    f.write(key + "," + str(value) + "\n")
    print(key + "," + str(value))
        
  
  

代码 2

import jieba

f = "红楼梦.txt"
sf = "停用词.txt"

fi1 = open(f,"r")
data = fi1.read()
words = jieba.lcut(data)
fi1.close()

fi2 = open(sf,"r")
stopword = fi2.read().split("\n")
fi2.close()

namelist = [[],[],[],[],[],[],[],[]]

for i in range(len(words)):
    if words[i] in stopword:
        words[i]=""
    for j in range(8):
        if words[i] in namelist[j]:
            words[i] = namelist[j][0]

#词频统计

d = {}
for word in words:
    d[word]=d.get(word,0)+1
lt = list(d.items())
lt.sort(key=lambda x:x[1], reverse=True)


#写入新文件

fo = open("result.csv","w")
for i in range(len(lt)):
    if lt[i][1] >= 40 and len(lt[i][0])>=2:
        fo.write("{},{}".format(lt[i][0],lt[i][1]))
        fo.write("\n")
fo.close()


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

guan12319

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值