python基础
- 1.学习目的
- 2.实验
- 2.1实验一
- 2.2实验二
- 3.实验代码及结果
1.学习目的
python库很多,所以我们要在空闲之余学习我们没有遇到的库,就比如turtle和分词库
2.实验
2.1实验一
2.2实验二
3.实验代码及结果
# @File: turtle库.py
# @Author: chen_song
# @Time: 2024/9/25 下午7:57
import turtle as t
ls = [69,292,33,131,61,254]
X_len = 400
Y_len = 300
x0 = -200
y0 = -100
t.penup()
# 前往目标点
t.goto(x0,y0)
t.pendown()
t.fd(X_len)
# 回到目标起点
t.fd(-X_len)
# 画笔方向设置为90°
t.seth(90)
t.fd(Y_len)
t.pencolor('red')
t.pensize(5)
for i in range(0,len(ls)):
t.penup()
# -100 Y轴0点
t.goto(x0+(i+1)*50,-100)
t.seth(90)
t.pendown()
t.fd(ls[i])
# 获取屏幕信息
screen = t.getscreen()
# @File: 词频统计以及差异分析.py
# @Author: chen_song
# @Time: 2024/9/25 下午8:41
import jieba
def fenci(txt):
f=open(txt,'r')
datas=f.read()
f.close()
data=jieba.lcut(datas)
d = {}
for i in data:
if len(i)>=2:
d[i]=d.get(i,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
return lt
def show(lt):
for i in lt[:9]:
print(i[0],':',i[1],end=',',sep='')
print(lt[9][0],':',lt[9][1],sep='')
l1=fenci('./data/data2018.txt')
l2=fenci('./data/data2019.txt')
print('2019:',end='',sep='')
show(l2)
print('2018:',end='',sep='')
show(l1)
# @File: 文章差异词语分析.py
# @Author: chen_song
# @Time: 2024/9/25 下午9:01
#
import jieba
def fenci(txt):
f=open(txt,'r')
datas=f.read()
f.close()
data=jieba.lcut(datas)
d = {}
for i in data:
if len(i)>=2:
# 计数+1
d[i]=d.get(i,0)+1
lt = list(d.items())
lt.sort(key = lambda x:x[1],reverse = True)
ls = [x[0] for x in lt[:10]]
return ls
def show(lt):
print(','.join(lt))
l1=fenci('./data/data2018.txt')
l2=fenci('./data/data2019.txt')
l3=[]
for i in l1:
if i in l2:
l3.append(i)
for i in l3:
l1.remove(i)
l2.remove(i)
print('共有词语:',end='',sep='')
show(l3)
print('2019特有:',end='',sep='')
show(l2)
print('2018特有:',end='',sep='')
show(l1)