当前位置:   article > 正文

计算机二级(Python)应用题代码自用(无忧二级)

计算机二级(Python)应用题代码自用(无忧二级)

简单应用题

注:基础操作和简单应用1过于简单涉及格式化输出、random库、以及turtle画图库等所以未展示

第一套 2

在这里插入图片描述

data = input()  # 姓名 年龄 性别
s = 0
n = 0
i = 0
while data:
    i += 1
    ls = data.split()
    s += int(ls[2])
    if ls[1] == '男':
        n += 1
    data = input()
s = s/i
print("平均年龄是{:.2f} 男性人数是{}".format(s,n))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
第二套 2

在这里插入图片描述

names=input("请输入各个同学行业名称,行业名称之间用空格间隔(回车结束输入):")
t = names.split()
d = {}
for i in range(len(t)):
    d[t[i]] = d.get(t[i],0)+1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 按照数量排序
for k in ls:
    print("{}:{}".format(k[0],k[1]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
第三套 2

在这里插入图片描述

# 与上一套极度相似
txt = input("请输入类型序列: ")
t = txt.split()
d = {}
for i in range(len(t)):
    d[t[i]] = d.get(t[i],0)+1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True)  # 按照数量排序
for k in ls:
    print("{}:{}".format(k[0], k[1]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
第四套 2

在这里插入图片描述

data = input()  # 课程名 考分
d = {}
while data:
    t = data.split() # t = [课程名,考分]
    d[t[0]] = t[1]
    data = input()
ls = list(d.items())
ls.sort(key = lambda x:x[1],reverse = True)
avg = 0
for i in d.values():
    avg += int(i)
avg = avg/len(ls)
print("最高分课程是{} {}, 最低分课程是{} {}, 平均分是{:.2f}".format(ls[0][0],ls[0][1],ls[-1][0],ls[-1][1],avg))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
第五套 2

在这里插入图片描述

f=open("name.txt")
names=f.readlines()
f.close()
f=open("vote.txt")
votes=f.readlines()
f.close()
f.close()
f=open("vote1.txt","w")
D={}
NUM=0
for vote in votes:
    num = len(vote.split())
    if num==1 and vote in names:  # 判断是否为有效票
        D[vote[:-1]]=D.get(vote[:-1],0)+1 # 次数统计
        NUM+=1
    else:
        f.write(vote)
f.close()        
l=list(D.items())
l.sort(key=lambda s:s[1],reverse = True)
name=l[0][0]
score=l[0][1]
print("有效票数为:{} 当选村长村民为:{},票数为:{}".format(NUM,name,score))


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
第六套 2

在这里插入图片描述

# 第一小问
f = open("vote.txt")
names = f.readlines()
f.close()
n = 0
for name in names:
    num = len(name.split()) #判断长度
    if num == 1:
        n+=1 #此处不可空格分开,挺傻逼的
print("有效票{}张".format(n))


# 第二小问
f = open("vote.txt")
names = f.readlines()
f.close()
D = {}
for name in names:
    if len(name.split())==1:
        D[name[:-1]]=D.get(name[:-1],0) + 1
l = list(D.items())
l.sort(key=lambda s:s[1],reverse = True)
name = l[0][0]
score = l[0][1]
print("最具人气明星为:{},票数为:{}".format(name,score))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
第七套 2

在这里插入图片描述

#请补充若干行代码,完成向量积运算
ls = [111, 222, 333, 444, 555, 666, 777, 888, 999]
lt = [999, 777, 555, 333, 111, 888, 666, 444, 222]
s = 0
for i in range(len(ls)):
    s += ls[i]*lt[i]
print(s)
 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
第八套 2

在这里插入图片描述

while True:
    s = input("请输入不带数字的文本:")
    n = 0
    for i in s:
        if '0' <= i <='9':
            n += 1
    if n == 0:
        break
print(len(s))

# 解法二
while True:
    s = input("请输入不带数字的文本:")
    flag = True
    for i in s:
        if '0' <= i <='9':
            flag = False
    if flag == True:
        break
print(len(s))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
第九套 2

在这里插入图片描述

import random
random.seed(2)

pdict= {'Alice':['123456789'],
        'Bob':['234567891'],
        'Lily':['345678912'],
        'Jane':['456789123']}

name = input('请输入一个人名:')
if name in pdict:
    print(name,pdict.get(name)[0],random.randint(1000,9999))
else:
    print('对不起,您输入的用户信息不存在。')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
第十套 2

在这里插入图片描述

img = [0.244, 0.832, 0.903, 0.145, 0.26, 0.452]
filter = [0.1,0.8,0.1]
res = []
for i in range(len(img)-2):
    k = 0
    for j in range(3):
        k += img[i+j]*filter[j]
        print('k={:<10.3f},filter[{}]={:<10.3f},img[{}+{}]={:<10.3f}'.format(k,j,filter[j],i,j,img[i+j]))
    res.append(k)
for r in res:
    print('{:<10.3f}'.format(r),end = '')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
第十一套 2

在这里插入图片描述


f = open('data.txt','r')

school = []
country = []
for line in f:
    lines = line.strip('\n').split(",")  # lines = ['1','麻省理工','美国']
    if lines != ['']:
        country.append(lines[-1])  # country = ['美国']
        school.append(lines[1:3])  # school = [['麻省理工','美国']]
    
country =list(set(country))

unis = []

for i in country:
    n = 0   #计算一个国家有多少个学校
    schools = [] # 存放同一个国家的学校
    result = []  # 存放一个国家的信息包括 国家名、学校个数、学校名
    for k in school:
        if k[1] == i:
            schools.append(k[0])
            n += 1
    result = [i, n ,schools]
    unis.append(result)

    
for d in unis:
    print('{:>4}: {:>4} : {}'.format(d[0],d[1],' '.join(d[2])))


f.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
第十二套 2

在这里插入图片描述

sumtime = 0
percls = []
ts = {}
with open('out.txt', 'r') as f:
    for line in f.readlines():
        line = line.strip('\n').split(',')
        sumtime += eval(line[1])
        ts[line[0]] = line[2]

        
print('the total execute time is ', sumtime)

tns = list(ts.items())
tns.sort(key=lambda x: x[1], reverse=True)
for i in range(3):
    print('the top {} percentage time is {}, spent in "{}" operation'.format(i, tns[i][1],tns[i][0]))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
第十三套 2

在这里插入图片描述

import jieba
s = input("请输入一段中文文本,句子之间以逗号或句号分隔:")
slist = jieba.lcut(s)
m = 0
for i in slist:
	if i in ",。":
        continue
   	print(i,end = '/')
   	m += 1
print("\n中文词语数是:{}\n".format(m))

s = ''
for i in slist:
   	if i in ",。":
      	print('{:^20}'.format(s))
      	s = ''
      	continue
   	s += i
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
第十四套 2

在这里插入图片描述

import random as r
r.seed(0)
persons = ['Aele', 'Bob','lala', 'baicai']
flag = 3
while flag>0:
    flag -= 1
    
    name = input("请输入一个名字:")
    if name in persons:
        num = r.randint(1000,9999)
        print('{} {}'.format(name, num))
    elif name == 'q':
        break
    else:
        print('对不起,您输入的名字不存在。')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
第十五套 2

在这里插入图片描述

import jieba

f = open('data.txt','w')

s = input("请输入一个中文字符串,包含逗号和句号:")
k=jieba.lcut(s)
d1 = {}
for i in k:
    if len(i) >= 2:
        d1[i] = d1.get(i,0) + 1
ls = list(d1.items())  # ls = [['键','值'],[],[]]
for i in range(len(ls)):
    f.write('{}:{}'.format(ls[i][0],ls[i][1]))
    f.write('\n')
    
f.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
第十六套 2

在这里插入图片描述

import jieba
dela = '-;:,.()"<>'
s = input("请输入一句话:")
print("\n这句话是:{}".format(s))
new = ""
for i in s:
    if i in dela:
        i = ""
    new += i
print("替换之后是:{}".format(new))
print("里面有 {}个词语。".format(len(jieba.lcut(new))))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

综合应用题

第一套

在这里插入图片描述

								# 第一问
f = open('命运.txt','r')

d = {}
for i in f.read():
    if i not in ",。 ? ! 《》 【】 “” ‘’":
        d[i] = d.get(i,0) + 1
ls = list(d.items())
ls.sort(key = lambda x:x[1],reverse = True)
print("{}:{}".format(ls[0][0],ls[0][1]))

f.close()

								# 第二问
f = open('命运.txt','r')
d = {}
for i in f.read():
    if i not in "\n ":
        d[i] = d.get(i,0) + 1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 此行可以按照词频由高到低排序

for  i in range(10):
    print(ls[i][0],end = "")

f.close()

								# 第三问	
fi = open('命运.txt','r')
fo = open('命运-频次排序.txt','w')
d = {}
for i in fi.read():
    if i not in "\n ":
        d[i] = d.get(i,0) + 1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 此行可以按照词频由高到低排序

s = ""
for k in ls:
    s +='{}:{}'.format(k[0],k[1]) + ','

fo.write(s[:-1])
#for i in ls:
#    fo.write('{}:{},'.format(i[0],i[1]))
fi.close()

fo.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
第二套

在这里插入图片描述

                                    # 第一问
fi = open('sensor.txt','r',encoding='utf-8')
fo = open('earpa001.txt','w')

for line in fi:
    ls = line.strip('\n').split(',')
    if ls[1] == ' earpa001':
        fo.write('{},{},{},{}\n'.format(ls[0],ls[1],ls[2],ls[3]))


fi.close()
fo.close()

                                    # 第二问
fi = open('earpa001.txt','r')
fo = open('earpa001_count.txt','w')
d = {}
for line in fi:
    ls = line.strip('\n').split(',')
    m = ls[2] + '-' + ls[3]
    d[m] = d.get(m,0) + 1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=False) # 该语句用于排序

# 写入文件
for i in range(len(ls)):
    fo.write('{},{}\n'.format(ls[i][0],ls[i][1]))

fi.close()
fo.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
第三套

在这里插入图片描述

# 第一问
fi = open("论语.txt", 'r')
fo = open("论语-原文.txt", 'w')
flag = False
for line in fi:
    if "【原文】" in line:
        flag = True
        continue
    if "【注释】" in line:
        flag = False
    line = line.strip(" \n")
    if flag:
        if line:
            fo.write(line + '\n')
fi.close()
fo.close()
# 第二问
fi = open("论语-原文.txt", 'r')
fo = open("论语-提纯原文.txt", 'w')

for line in fi:
    for i in range(1,100):
        line=line.replace('({})'.format(i),'')
    fo.write(line)
fi.close()
fo.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
第四套

在这里插入图片描述

# 第一问
f = open('PY301-SunSign.csv','r')
lines = f.readlines()
name = input('请输入星座中文名称(例如,双子座):')
ls = []
for line in lines:
    if name in line:
        ls = line.split(',')
        print("{}的生日位于{}-{}之间".format(ls[1],ls[2],ls[3]))
f.close()
# 第二问
f = open('PY301-SunSign.csv','r')
lines = f.readlines()
s = input('请输入星座序号(例如,5):')
nums = s.split()
ls = []
for num in nums:
    ls = lines[eval(num)].split(',')
    print("{}({})的生日是{}月{}日至{}月{}日之间".format(ls[1],ls[4][:-1],ls[2][:-2],ls[2][-2:],ls[3][:-2],ls[3][-2:]))
s = input('请输入星座序号(例如,5):')
f.close()
# 第三问
f = open('PY301-SunSign.csv','r')
lines = f.readlines()
s = input('请输入星座序号(例如,5):')
nums = s.split()
ls = []
for num in nums:
      if 1<= eval(num) <= 12:
            ls = lines[eval(num)].split(',')
            print("{}({})的生日是{}月{}日至{}月{}日之间".format(ls[1],ls[4][:-1],ls[2][:-2],ls[2][-2:],ls[3][:-2],ls[3][-2:]))
      else:
            print('输入星座序号有误!')
s = input('请输入星座序号(例如,5):')
f.close()
	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
第五套

在这里插入图片描述

# 第一问
import jieba
f = open('data.txt','r')   
lines = f.readlines()
f.close()
f = open('out.txt','w')    
for line in lines:     
    line = line.strip(' ')              #删除每行首尾可能出现的空格
    wordList = jieba.lcut(line)          #用结巴分词,对每行内容进行分词
    f.writelines('\n'.join(wordList))  #将分词结果存到文件out.txt中
f.close()

# 第二问
import jieba
f = open('out.txt','r')    #以读的方式打开文件
words = f.readlines()
f.close()
D={}
for w in words:        #词频统计
    D[w[:-1]]=D.get(w[:-1],0) + 1
print("曹操出现次数为:{}  ".format(D["曹操"]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
第六套

在这里插入图片描述

# 第一问
import jieba  #此处可多行
fi = open('data.txt','r')
lines = fi.readlines()
f = open('out1.txt','w')

words = []
for line in lines:
    line = line.strip('\n')
    wordlist = jieba.lcut(line)
    for word in wordlist:
        if len(word) >= 3 and (word not in words):
            words.append(word)
for word in words:
    f.write(word + '\n')

fi.close()
f.close()

# 第二问
import jieba # 此处可多行
fi = open('data.txt','r')
lines = fi.readlines()
f = open('out2.txt','w')

words = []
for line in lines:
    line = line.strip('\n')
    wordlist = jieba.lcut(line)
    for word in wordlist:
        if len(word) >= 3:
            words.append(word)
d = {}
for word in words:
    d[word] = d.get(word,0) + 1

ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 此行可以按照词频由高到低排序

s = ''
for i in ls:
    s = '{}:{}'.format(i[0],i[1])
    f.write(s+'\n')
fi.close()
f.close()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
第七套

在这里插入图片描述

# 第一问
fi = open("score.txt","r")
lines = fi.readlines()
fo = open('candidate0.txt','w')
D=[]  #单个学生的数据
L=[]  #L中的元素是学生原始成绩和总成绩
for line in lines:
    D=line.split()
    s=0  #每个学生的总成绩初始值
    for i in range(10):
        s+=int(D[i+2]) #各科成绩累加求和,+2是因为前两个元素是学号和姓名
    D.append(s)
    L.append(D)

L.sort(key=lambda x:x[-1],reverse=True)   #按学生总成绩从大到小排序

for i in range(10):  #取前十个学生数据
    for j in range(len(L[i])): #一个学生的各项数据
        fo.write('{} '.format(L[i][j])) #写各项数据,用空格隔开
    fo.write('\n')  #换行
fi.close()
fo.close()
# 第二问
fi=open("candidate0.txt",'r')
lines=fi.readlines()
D=[]
fo=open('candidate.txt','w')
for line in lines:
    D=line.split()
    for i in range(10):
        if int (D[i+2])<0:
            break
    else:
        fo.write('{}{}\n'.format(D[0],D[1]))
fi.close()
fo.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
第八套

在这里插入图片描述

# 第一问
fi = open('data.txt','r')  # 此处可多行
lines = fi.readlines()
fo = open("univ.txt", "w")

for line in lines:
    if 'alt' in line:
        dx = line.split('alt=')[1].split('"')[1]
        fo.write('{}\n'.format(dx))

fi.close()
fo.close()

# 第二问
n = 0

k = 0

f = open("univ.txt", "r")

lines = f.readlines() # 此处可多行

f.close()

for line in lines:
    if '大学生' in line:
        continue
    elif "大学" in line:
            n += 1
            print(line)
    elif "学院" in line:
            k += 1
            print(line)

print("包含大学的名称数量是{}".format(n))
print("包含学院的名称数量是{}".format(k))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
第九套

在这里插入图片描述

# 第一问
import jieba
f2019 = open('data2019.txt','r')
f2018 = open('data2018.txt','r')
lines2019 = f2019.read().split('\n')
lines2018 = f2018.read().split('\n')

d = {}

for i in lines2019:
    word = jieba.lcut(i)
    for j in word:
        if len(j) >=2:
            d[j] = d.get(j,0) + 1

lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print('2019',end='')
for i in range(9):
    print('{}:{},'.format(lt[i][0],lt[i][1]),end = '')
print('{}:{}'.format(lt[9][0],lt[9][1]))

d = {}

for i in lines2018:
    word = jieba.lcut(i)
    for j in word:
        if len(j) >=2:
            d[j] = d.get(j,0) + 1

lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print('2018',end='')
for i in range(9):
    print('{}:{},'.format(lt[i][0],lt[i][1]),end = '')
print('{}:{}'.format(lt[9][0],lt[9][1]))
f2019.close()
f2018.close()
# 第二问
import jieba
f2019 = open('data2019.txt','r')
f2018 = open('data2018.txt','r')
lines2019 = f2019.read().split('\n')
lines2018 = f2018.read().split('\n')
f2019.close()
f2018.close()

d = {}
for i in lines2019:
    word = jieba.lcut(i)
    for j in word:
        if len(j) >=2:
            d[j] = d.get(j,0) + 1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
lt2019 = []
for i in range(10):
    lt2019.append(lt[i][0])
    
d = {}
for i in lines2018:
    word = jieba.lcut(i)
    for j in word:
        if len(j) >=2:
            d[j] = d.get(j,0) + 1

lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
lt2018 = []
for i in range(10):
    lt2018.append(lt[i][0])
ltlike = []
for i in lt2019:
    if i in lt2018:
        ltlike.append(i)
for i in ltlike:
    lt2019.remove(i)
    lt2018.remove(i)

print('共有词语:',end = '')
for i in ltlike[:-1]:
    print(i,end = ',')
print(ltlike[-1])

print('2019特有:',end = '')
for i in lt2019[:-1]:
    print(i,end = ',')
print(lt2019[-1])
print('2018特有:',end = '')
for i in lt2018[:-1]:
    print(i,end = ',')
print(lt2018[-1])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
第十套

在这里插入图片描述

# 第一问
fi = open('data.txt','r',encoding = 'utf-8')
data = fi.read()
fi.close()
fo = open('clean.txt','w')

s = ''
x = ",。:;、?!()  \n"
for i in data:
    if i not in x:
        s += i
fo.write(s)
fo.close()
# 第二问
import jieba
fi = open('clean.txt','r')
data = fi.read()
word = jieba.lcut(data)
fi.close()

d = {}
for i in word:
    if len(i) >= 3:
        d[i] = d.get(i,0) + 1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
for i in range(10):
    if i < 9:
        print('{}:{},'.format(lt[i][0],lt[i][1]),end='')
    else:
        print('{}:{}'.format(lt[i][0],lt[i][1]))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
第十一套

在这里插入图片描述

import jieba

f = "红楼梦.txt"
sf = "停用词.txt"
fi = open(f ,'r' ,encoding = 'utf-8')
data = fi.read()
words = jieba.lcut(data)
fi.close()
fs = open(sf,'r',encoding = 'utf-8')
stopword = fs.read().split('\n')
fs.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 i in words:
    d[i] = d.get(i,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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
第十二套

在这里插入图片描述

# 第一问
fi = open("八十天环游地球.txt",'r')
fo = open("八十天环游地球-章节.txt",'w')
words =  fi.read().split('\n')

for word in words:
    if '第' in word and '章' in word and len(word) <50 :
        fo.write(word)
        fo.write('\n')
fi.close()
fo.close()
# 第二问
import jieba

fi1 = open('八十天环游地球-章节.txt','r')
name = fi1.read().split('\n') # 章节名
fi1.close()
fi2 = open('八十天环游地球.txt','r')
nr = fi2.read().split('\n') # 内容
fi2.close()

n = 0
k = 1
tem = []
for i in nr:
    if i in name:
        n += 1
    if n == k:
        newword = jieba.lcut(i)
        for i in newword:
            tem.append(i)
    else:
        d ={}
        for i in tem:
            if len(i) >= 2:
                d[i] = d.get(i,0)+1
        lt = list(d.items())
        lt.sort(key = lambda x:x[1],reverse = True)
        c = name[k-1].split(' ')
        print('{} {} {}'.format(c[0],lt[0][0],lt[0][1]))

        k += 1
        tem = []
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
第十三套

在这里插入图片描述

# 第一问
fi = open('data.txt','r')
data = fi.read().strip().split('\n')
fi.close()

fo = open('student.txt','w')
for i in data:
    fo.write('{}:{}'.format(i[:3],i[-3:]))
    fo.write('\n')
fo.close()

# 第二问
fi = open('studs.txt','r')
data = fi.read().strip().split('\n')
fi.close()
names = []
for i in data:
    t = i.split(':')
    names.append(t)

d = {}
for i in names:
    d[i[0]] = i[1]
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print("{}:{}".format(lt[0][0],lt[0][1]))
# 第三问
f = open('data.txt','r')
data = f.read().strip().split('\n')
f.close()
student = []
for i in data:
    t1 = i.split(':')
    t2 = t1[1].split(',')
    if t2[0] not in student:
        student.append(t2[0])

gs = 0
n = 0
for i in student:
    for j in data:
        t1 = j.split(',')
        if i in t1[0]:
            gs += eval(t1[1])
            n += 1
    print("{}:{:.2f}".format(i,gs/n))
    gs = 0
    n = 0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
第十四套

在这里插入图片描述

# 第一问
fi = open('webpage.txt','r')
p = fi.read().split()
fi.close()

n = 0
for i in p:
    if 'JPG' in i:
        n += 1

print(n)
# 第二问
fi = open('webpage.txt','r')
p = fi.read().split('\n')
fi.close()

fo = open('images.txt','w')

for i in p:
    if '.JPG' in i:
        start = i.index('src=') + 5
        end = i.index('.JPG') + 4
        fo.write(i[start:end])
        fo.write('\n')

fo.close()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
第十五套

在这里插入图片描述

# 第一问

import jieba
f = open('data3.txt','r')
lines = f.readlines()
f.close()
d ={}
words = []
for i in lines:
    word = jieba.lcut(i)
    for j in word:
        words.append(j)

for i in words:
    if len(i) >= 2:
        d[i] = d.get(i,0) + 1

lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
for i in range(10):
    print('{}:{}'.format(lt[i][0],lt[i][1]))
# 第二问
fi = open('data3.txt','r')
fo = open('out.txt','w')
lines = fi.read().strip().split('。')
line = []
for i in lines:
    t = i.strip().split(',')
    for j in t:
        if '我们' in j:
            line.append(j)
for i in line:
    fo.write(i)
    fo.write('\n')
fi.close()
fo.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
第十六套

在这里插入图片描述

# 第一问
fi = open("data301.txt","r")
f = open("result301.txt","w")
cnumd = {}
name = ''
count = 0
flag = 1
for line in fi:
    if '"name":' in line:
        name = line.split(":")[1][2:-3]
        flag = 1
    elif '"value":' in line and flag == 1:
        dx = line.split(":")[1][0:-1]
        # dx = line.split(":")[1].strip()
        cnumd[name] = dx
        flag = 0
        count += 1
for d in cnumd.items():
    f.write("{}:{}\n".format(d[0],d[1]))
print("一共有{}个国家".format(count))
f.close()
fi.close()
# 第二问
lcnum = []
with open("result301.txt", "r") as fi:
    for line in fi:
        c = line.split(":")
        c[1]= eval(c[1])
        lcnum.append(c)
        
lcnum.sort(key=lambda x:x[1], reverse= True)
lz = 0
lw = 0
for l in lcnum:
    if l[1] > 10000:
        lw += 1
    elif l[1] == 0:
        lz += 1

print("新冠确诊人数最多的国家是{},人数是{}".format(lcnum[0][0],lcnum[0][1] ))
print("新冠确诊人数超过1W的国家有{}个".format(lw))
print("新冠确诊人数为0的国家有{}个".format(lz))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/123111
推荐阅读
相关标签
  

闽ICP备14008679号