计算机二级

1.turtle库的使用

在这里插入图片描述

import turtle
turtle.pensize(2)
d = -45
for i in range(4):
    turtle.seth(d)
    d  += 90
    turtle.fd(200)

turtle库进阶
turtle库入门

2.jieba库的使用

在这里插入图片描述
jieba

3.random库的使用

random

4.文件读取操作

题目
下面所示为一套由公司职员随身佩戴的位置传感器采集的数据,文件名为"sensor.txt",其内容示例如下:

2016/5/31 0:05, vawelon001,1,1
2016/5/31 0:20, earpa001,1,1
2016/5/31 2:26, earpa001,1,6

…(略)

第一列是传感器获取数据的时间,第二列是传感器的编号,第三列是传感器所在楼层,第四列是传感器所在位置区域编号。

1.在PY301_1.py文件中修改代码,读入sensor.txt文件中的数据,提取出传感器编号为arpa001的所有数据,将结果输出保存到“earpa001.txt”文件。
输出文件格式要求:原数据文件中的每行记录写入新文件中,行尾无空格,无空行。参考格式如下:
2016/5/31 7:11, arpa001,2,4
2016/5/31 8:02, arpa001,3,4
2016/5/31 9:22, arpa001,3,4
…(略)

fi=open('sensor.txt','r',encoding='utf-8')
fo=open('earpa001.txt','w')
txt=fi.readlines()
for line in txt:
    temp=line.strip().split(',')
    if temp[1].strip()=='earpa001':
        fo.write('{},{},{},{}\n'.format(temp[0],temp[1].strip(),temp[2],temp[3]))
fi.close()
fo.close()

2.在PY301_2.py文件中修改代码,读入“ earpa001.txt”文件中的数据,统计 earpa001对应的职员在各楼层和区域出现的次数,保存到“ earpa001_count.txt”文件,每条记录一行,位置信息和出现的次数之间用英文半角逗号隔开,行尾无空格,无空行。参考格式如下
1-1,5
1-4,3
…(略)
含义如下:第1行“1-1,5”中1-1表示1楼1号区域,5表示出现5次;第2行“1-4,3”中1-4表示1楼4号区域,3表示出现3次;

fp=open('earpa001.txt','r',encoding='utf-8')
fo=open('earpa001_count.txt','w')
txt=fp.readlines()
d = {}
for line in txt:
    temp=line.split(',')
    d['{}-{}'.format(temp[2],temp[3]).strip()]=d.get('{}-{}'.format(temp[2],temp[3]).strip(),0)+1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 该语句用于排序
for line in ls:
  fo.write('{},{}\n'.format(line[0],line[1]))
fp.close()
fo.close()

4.format()函数

format()函数

5.文件操作

文件操作入门
在这里插入图片描述
问题1:

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==True:
        if line:
            fo.write(line+'\n')
fi.close()
fo.close()

问题2:

fi = open("论语-原文.txt", 'r')
fo = open("论语-提纯原文.txt", 'w')
for line in fi:
    for i in range(24):
        line=line.replace('({})'.format(i),'')
    fo.write(line)
fi.close()
fo.close()

第7套
46、某班学生评选一等奖学金,学生的10门主干课成绩存在考生文件夹下文件score.txt中,每行为一个学生的信息,分别记录了学生学号、姓名以及10门课成绩,格式如下:

1820161043 郑珉镐 68 66 83 77 56 73 61 69 66 78

1820161044 沈红伟 91 70 81 91 96 80 78 91 89 94

……

从这些学生中选出奖学金候选人,条件是:1)总成绩排名在前10名;2)全部课程及格(成绩大于等于60)。

问题1:给出按总成绩从高到低排序的前10名学生名单,并写入文件candidate0.txt,每行记录一个学生的信息,分别为学生学号、姓名以及10门课成绩。补充考生文件夹下文件PY301-1.py,完成这一功能。

问题2:读取文件candidate0.txt,从中选出候选人,并将学号和姓名写入文件candidate.txt格式如下:

1010112161722 张三

1010112161728 李四

……

补充考生文件夹下文件PY301-2.py.完成这一功能。
问题一:

fi=open('score.txt','r')  #此处可多行
fo=open('candidate0.txt','w')
ls=fi.readlines()
L=[]
D=[]
for line in ls:
    s=0
    D=line.strip('\n').split()
    for i in D[2:]:
        s+=int(i)
    D.append(s)
    print(D)
    L.append(D)
L.sort(key=lambda x:x[-1],reverse=True)   #按学生总成绩从大到小排序
for k in range(10):
    fo.write(' '.join(L[k][:-1])+'\n')
fi.close()
fo.close()

如果for循环正常结束,则执行else下的语句。
如果循环中遇到break退出了,则不会执行到else下的语句

fi=open('candidate0.txt','r')
fo=open('candidate.txt','w')
ls=fi.readlines()
for line in ls:
    It=line.split()
    for i in It[2:]:
        if int(i)<60:
            break
    else:
        fo.write(' '.join(It[:2])+'\n')
fi.close()
fo.close()

第6套:
在这里插入图片描述
第1题

import jieba
f=open('data.txt','r')
lines=f.readlines()
f.close()
D=[]
for line in lines:
    wordList=jieba.lcut(line)
    for word in wordList:
        if len(word)>=3:
            if word not in D:
                D.append(word)
f=open('out1.txt','w')
print(D)
f.writelines('\n'.join(D))
f.close()               

第2题

import jieba
f=open('data.txt','r')
txt=f.read()
fo=open('out2.txt','w')
txt=jieba.lcut(txt)
d = {}
for i in txt:
    d[i]=d.get(i,0)+1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 此行可以按照词频由高到低排序
for i in range(len(ls)):
    if len(ls[i][0])>=3:
        fo.write("{}:{}\n".format(ls[i][0],ls[i][1]))
fo.close()

第8套:
在这里插入图片描述
在这里插入图片描述
第一题:

f=open('data.txt','r')
txt=f.read().splitlines()
f.close()
f = open("univ.txt", "w")
for line in txt:
    if 'alt' in line:
        dx=line.split('alt=')[-1].split('"')[1]
        f.write('{}\n'.format(dx))
f.close()

第二题:

n=0
m=0
f = open("univ.txt", "r")
txt=f.readlines()
for line in txt:
    if '大学生' in line:
        continue
    elif '大学' in line:
        n+=1
        print("{}".format(line))
    elif '学院' in line:
        m+=1
        print("{}".format(line))
f.close()
print("包含大学的名称数量是{}".format(n))
print("包含学院的名称数量是{}".format(m))

第9套:(有点麻烦)
在这里插入图片描述

import jieba
f=open('data2019.txt')
txt=f.read()
f.close()
txt=jieba.lcut(txt)
d = {}
for i in txt:
    if len(i)<2:
        continue
    d[i]=d.get(i,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print("2019:",end='')
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]))
f=open('data2018.txt')
txt=f.read()
f.close()
txt=jieba.lcut(txt)
d = {}
for i in txt:
    if len(i)<2:
        continue
    d[i]=d.get(i,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print("2018:",end='')
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]))

注意是主题词

import jieba
fa=open('data2019.txt','r')
txt=fa.read()
fa.close()
words=jieba.lcut(txt)
d={}
for word in words:
    if len(word)==1:
        continue
    else:
        d[word]=d.get(word,0)+1
lt=list(d.items())
lt.sort(key=lambda x:x[1],reverse=True)
da={}
for i in range(10):
    da[i]=lt[i][0]
#求2018中的10个主题词
fb=open('data2018.txt','r')
txt=fb.read()
fb.close()
words=jieba.lcut(txt)
d={}
for word in words:
    if len(word)==1:
        continue
    else:
        d[word]=d.get(word,0)+1
lt=list(d.items())
lt.sort(key=lambda x:x[1],reverse=True)
db={}
for i in range(10):
    db[i]=lt[i][0]
#求m个共有词存入gy,并将da,db中原共有的改为空
gy={}
m=0
for i in range(10):
    for j in range(10):
        if da[i]==db[j]:
            gy[m]=da[i]
            da[i]=''
            db[j]=''
            m+=1
            break
print("共有词语:",end='')
for i in range(m):
    if i<m-1:
        print("{}".format(gy[i]),end=',')
    else:
        print("{}".format(gy[i]))
print("2019特有:",end='')
j=0
for i in range(10):
    if da[i]!="":
        if j<10-m-1:
            print("{}".format(da[i]),end=',')
        else:
            print("{}".format(da[i]))
        j+=1
print("2018特有:",end='')
j=0
for i in range(10):
    if db[i]!="":
        if j<10-m-1:
            print("{}".format(db[i]),end=',')
        else:
            print("{}".format(db[i]))
        j+=1
              

第11套:
在这里插入图片描述
在这里插入图片描述

import jieba
f = "红楼梦.txt"
sf = "停用词.txt"
txt=jieba.lcut(open(f,'r',encoding='utf-8').read())
stop_words=[]
with open(sf,'r',encoding='utf-8') as f:
    #for i in f.read().splitlines():
        #stop_words.append(i)
    stop_words=f.read().splitlines()
#剔除停用词
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=='林妹妹' or word=='黛玉道':
        rword='黛玉'
    elif word=='宝钗' or word=='宝丫头':
        rword='宝钗'
    elif word=='贾母' or word=='老祖宗':
        rword='贾母'
    elif word=='袭人' or word=='袭人道':
        rword='袭人'
    elif word=='贾政' or word=='贾政道':
        rword='贾政'
    elif word=='贾琏' or 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)
print(li)
#列出词频超过40的结果
with open('result.csv','a',encoding='gbk') as f:
    for i in li:
        key,value=i
        if value<40:
            break
        f.write(key+','+str(value)+'\n')
        print(key+','+str(value))

第16套

使用字典和列表型变量完成村长选举。某村有40名有选举权和被选举权的村民,名单由考生文件夹下文件name.txt给出,从这40名村民中选出一人当村长,40人的投票信息由考生文件夹下文件vote.txt给出, 每行是一张选票的信息,有效票中得票最多的村民当选。
问题一: 请从vote.txt中筛选出无效票写入文件vote1.txt。 有效票的含义是:选票中只有一个名字且该名字在name.txt文件列表中,不是有效票的票称为无效票。

问题二:给出当选村长的名字及其得票数

答案

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值