1. Python标准库中的GUI界面 turtle
turtle 的简单使用
# 导入turtle as 是给起一个别名
import turtle as t
# 设置画笔的大小为10px
t.pensize(10)
# 设置画笔颜色为蓝色
t.color('blue')
# 绘制NEUSOFT图标
# 抬笔,为了不留下痕迹
t.penup()
# 水平左移,参数为坐标
t.goto(-300, 0)
# 落笔
t.pd()
# 1.绘制N
# 首先向左旋转90度
t.left(90)
#然后画一条长度为80的线
t.forward(80)
#接着向右旋转145度
t.right(145) #rt:right
#简写
t.fd(100) #fd:forward
t.lt(145) #lt:left
t.fd(80)
# 2.绘制E
t.penup()
t.goto(-120, 80)
t.pd()
#"—"
t.lt(90)
t.fd(60)
#"|"
t.lt(90)
t.fd(80)
# 最后"—"
t.penup()
t.goto(-180, -5)
t.pd()
t.lt(90)
t.fd(60)
# 第二"—"
t.penup()
t.goto(-180, 40)
t.pd()
t.fd(60)
# 3.绘制U
# 右边的"|"
t.penup()
t.goto(-10, 10)
t.pd()
t.lt(90)
t.fd(70)
#
t.penup()
t.goto(-10, 10)
t.pd()
t.circle(26,-180)
# 左边的"|"
t.back(70)
# 4.绘制S
t.penup()
t.goto(50, 10)
t.pd()
t.circle(26,270) #上半边
# r 是正数时,angle 是负数时,反方向绘制圆
# r 是负数时,angle 是正数时,能画出s
t.circle(-22,270) #下半边
# 5.绘制O
t.penup()
t.goto(140, 30)
t.pd()
# 画圆,参数为半径、画笔转过的角度
t.circle(30)
# 6.绘制F
# "—"
t.penup()
t.goto(220, 80)
t.pd()
t.lt(90)
t.fd(60)
# "—"
t.penup()
t.goto(220, 40)
t.pd()
t.fd(60)
#"|"
t.penup()
t.goto(220, 80)
t.pd()
t.lt(90)
t.back(100)
# 7.绘制T
#"—"
t.penup()
t.goto(360, 80)
t.pd()
t.lt(90)
t.fd(60)
#"|"
t.penup()
t.goto(330, 80)
t.pd()
t.lt(90)
t.fd(100)
# 让gui界面一直显示,所有执行的代码应在此函数前
t.done()
2. for 循环
# for 临时变量 in 可迭代对象
range(起始位置,终止位置,步长) 左闭右开
for x in 'neusoft':
print(x)
for i in range(1,101):
print('这是我的第{}次问好'.format(i))
# 遍历
hero_name = ['鲁班七号','安琪拉','李白','刘备']
for hero in hero_name:
print(hero)
3.列表的访问
(1).列表名[索引]
print(hero_name[2])
(2).添加append
hero_name.append('后羿')
print('添加后的列表',hero_name)
(3).修改
hero_name[1] = 1000
print('修改后的列表',hero_name)
(4).删除
del hero_name[1]
print('删除后的列表',hero_name)
# 练习
# 创建[1,2,3......10]这样一个数字列表
# 1.创建空列表
# 2.使用for 循环,在循环中添加元素值
num_list = []
for i in range(1,11):
num_list.append(i)
print(num_list)
4.字符串
# 定义形式 '' " "
# 切片: 对序列截取一部分的操作,适用于列表
name = 'abcdefg'
# [起始位置:终止位置:步长] 左闭右开
# b c
print(name[1:3])
# a c e g
print(name[0:7:2])
# 全切片的时候可以省略初始和终止位置
print(name[::2])
#常见方法
#去掉两端空格
name = ' abcdefg '
# 查看序列内元素个数 len()
print(len(name))
name = name.strip()
print('去空格之后',len(name))
# 替换
price = '¥999'
price = price.replace('¥','$')
print(price)
# 列表变成字符串方法 join
li = ['a','b','c','d']# a = '-'.join(li)
print(a)
print(type(a))
5.元组
# 元组 tuple 元组和列表很像,只不过元组不可以修改
# 定义 ()
a = ('张三','李四','王五',1000)
print(a)
print(type(a))
#访问
print(a[3])
# 关于元组需要注意的是,()中只有一个元素时不是元组,要使它变成元组,要在该元素后面加逗号
b = ('lisi')
c = (1000)
d = ('lisi',)
print(type(b))
print(type(c))
print(type(d))
6.字典
# 字典 dict java hashmap
# key-value 数据结构
# 定义形式 { }
info = {'name':'李四','age':'34','addr':'重庆市'}
print(len(info))
print(info)
# 1.字典的访问
print(info['name'])
# 2.字典的修改
info['addr']='广东省'
print('修改后的字典',info)
# 3.增加
info['sex'] = 'female'
print('增加后的字典',info)
# 4.获取字典中所有的键
print(info.keys())
# 5.获取字典中所有的值
print(info.values())
# 6.获取字典中所有的key-value
print(info.items())
# 列表转字典
d = ([('name', '李四'), ('age', '34'), ('addr', '广东省'), ('sex', 'female')])
d1 = dict(d)
print(d1)
# 遍历字典
for k,v in info.items():
print(k,v)
7.集合
# 无序,不重复
set1 = {'zhangshan','lili',222}
print(type(set1))
print(set1)
# 列表的排序
#shuffle() 方法将序列的所有元素随机排序。
li = []
for i in range(10):
li.append(i)
print(li)
from random import shuffle
shuffle(li)
print('随机打乱的列表',li)
li.sort(reverse=True) #排序,正序
stu_info = [
{"name":'zhangsan',"age":18},
{"name":'lisi',"age":23},
{"name":'wangwu',"age":15},
{"name":'zhaoliu',"age":11},
]
print('排序前',stu_info)
# def 函数名(参数)
# 函数体
def sort_by_age(x):
return x['age']
# 根据年龄大小排序
stu_info.sort(key=sort_by_age)
print('排序后的列表',stu_info)
# 练习
name_info_list = [
('张三',4500),
('李四',9900),
('王五',2000),
('赵六',5500)
]
# 根据元组第二个元素进行正序排序
def sort_by_money(x):
return x[1]
name_info_list.sort(key=sort_by_money)
print('排序后的列表',name_info_list)
本地文件读取
# python中使用open内置函数进行文件读取
# 写法一
f = open(file='./novel/threekingdom.txt',mode= 'r',encoding = 'utf-8')
data = f.read()
print(data)
f.close()
# 写法二
# with as 上下文管理器 不用手动关闭流
with open('./novel/threekingdom.txt','r',encoding= 'utf-8') as f:
data = f.read()
print(data)
# 写入
txt = 'i like python'
with open('test.txt','w',encoding ='utf-8') as f:
f.write(txt)
text = """
Title
重庆师范欢迎你
"""
print(text)
with open('chongqingshifan.html','w',encoding='utf-8') as f:
f.write(text)
3.中文分词 jieba
# 指定国内镜像安装
# 1.在用户目录下新建pip文件夹
# 2.新建pip.ini文件
# 添加
"""
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
"""
# pip install jieba
# 导入jieba分词
import jieba
# 三种分词模式
seg = "我来到北京清华大学"
# (1)精确模式 精确分词
seg_list = jieba.lcut(seg)
print(seg_list)
# (2)全模式 找出所有可能的分词结果 冗余性大
seg_list1 = jieba.lcut(seg,cut_all=True)
print(seg_list1)
# (3)搜索引擎模式
seg_list2 = jieba.lcut_for_search(seg)
print(seg_list2)
text = '小明硕士毕业于中国科学院计算所,后在日本京都大学深造'
seg_list4 = jieba.lcut(text,cut_all=True)
print(seg_list4)
# 搜索引擎模式 先执行精确模式,在对其中的长词进行处理
seg_list5 = jieba.lcut_for_search(text)
print(seg_list5)
# nlp
4.词云
# 词云展示
#安装
# 在Terminal中输入 pip install wordcloud
from wordcloud import WordCloud
import jieba
import imageio
# # 绘制词云
text = 'He was an old man who fished alone in a skiff in the Gulf Stream and he had gone eighty-four days now without taking a fish. In the first forty days a boy had been with him. But after forty days without a fish the boy’s parents had told him that the old man was now definitely and finally salao, which is the worst form of unlucky, and the boy had gone at their orders in another boat which caught three good fish the first week. It made the boy sad to see the old man come in each day with his skiff empty and he always went down to help him carry either the coiled lines or the gaff and harpoon and the sail that was furled around the mast. The sail was patched with flour sacks and, furled, it looked like the flag of permanent defeat.'
wc = WordCloud().generate(text)
wc.to_file('老人与海.png')
# 三国演义小说词云绘制
#
# 三国演义小说分词
# 读取三国演义小说
mask = imageio.imread('./china.jpg')
with open('./novel/threekingdom.txt','r', encoding='utf-8') as f:
words = f.read()
print(len(words)) # 字数 55万
words_list = jieba.lcut(words)
print(len(words_list)) # 分词后的词语数 35万
print(words_list)
# 将words_list转化成字符串
novel_words = " ".join(words_list)
print(novel_words)
# WordCloud()里面设置参数
wc = WordCloud(
font_path='msyh.ttc', #字体
background_color='white',
width=800,
height=600,
mask=mask
).generate(novel_words)
wc.to_file('三国词云.png')