python计算机二级综合应用题,两年工作报告词频统计。

问题描述:

python计算机二级综合应用题,两年工作报告词频统计。

问题解答:

本人写的代码有点拙劣,运行效率可能不高,仅作为一种思路。

第一问:

# 
# 以下代码仅供参考。
# 
# 
# 以下代码仅供参考。
#
import jieba
f1=open("data2019.txt","r")
lines=f1.readlines()
d = {}
for line in lines:
    line = line.strip()
    line=jieba.lcut(line)
    for word in line:
        if len(word)>=2:
            d[word]=d.get(word,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print("2019:",end="")
for i in range(9):
    print(lt[i][0]+":"+str(lt[i][1]),end=",")
print(lt[9][0]+":"+str(lt[9][1]))



f2=open("data2018.txt","r")
lines=f2.readlines()
d = {}
for line in lines:
    line = line.strip()
    line=jieba.lcut(line)
    for word in line:
        if len(word)>=2:
            d[word]=d.get(word,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
print("2018:",end="")
for i in range(9):
    print(lt[i][0]+":"+str(lt[i][1]),end=",")
print(lt[9][0]+":"+str(lt[9][1]))

第二问:

# 
# 以下代码仅供参考。
# 

# 
# 以下代码仅供参考。
# 
import jieba
f1=open("data2019.txt","r")
lines=f1.readlines()
d = {}
for line in lines:
    line = line.strip()
    line=jieba.lcut(line)
    for word in line:
        if len(word)>=2:
            d[word]=d.get(word,0)+1
lt1 = list(d.items())
lt1.sort(key = lambda x:x[1],reverse = True)
set1 = set()
for i in lt1[0:10]:
    set1.add(i[0])

f2=open("data2018.txt","r")
lines=f2.readlines()
d = {}
for line in lines:
    line = line.strip()
    line=jieba.lcut(line)
    for word in line:
        if len(word)>=2:
            d[word]=d.get(word,0)+1
lt2 = list(d.items())
lt2.sort(key = lambda x:x[1],reverse = True)
set2 = set()
for i in lt2[0:10]:
    set2.add(i[0])

set_jiao=set1&set2
set_cha2019= set1 -set2
set_cha2018= set2 -set1
print("共有词语:" + ",".join(set_jiao))
print("2019特有:" + ",".join(set_cha2019))
print("2018特有:" + ",".join(set_cha2018))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

神笔馬良

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

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

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

打赏作者

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

抵扣说明:

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

余额充值