Python学习的第二天

turtle的使用

Python标准库中的GUI界面 -- > turtle

t.pensize(10)
t.color('blue')
t.speed(1)

t.penup()
t.goto(-260,200)
t.pd()

t.left(45)
t.forward(70)

t.penup()
t.goto(-260,160)
t.pd()

t.forward(70)

t.penup()
t.goto(-240,170)
t.pd()
t.right(135)
t.forward(120)

t.penup()
t.goto(-120,240)
t.pd()

t.right(60)
t.fd(80)

t.penup()
t.goto(-130,230)
t.pd()

t.left(130)
t.fd(60)

t.penup()
t.goto(-155,190)
t.pd()
t.left(20)
t.fd(40)

t.penup()
t.goto(-170,160)
t.pd()
t.fd(70)

t.penup()
t.goto(-135,190)
t.pd()
t.right(90)
t.fd(120)

t.penup()
t.goto(-135,70)
t.pd()
t.right(135)
t.fd(20)

t.penup()
t.goto(-170,110)
t.pd()
t.left(90)
t.fd(20)

t.penup()
t.goto(-110,110)
t.pd()
t.left(90)
t.fd(20)

#第二个字
#横
t.penup()
t.goto(-20,210)
t.pd()
t.left(45)
t.fd(120)

#横
t.penup()
t.goto(0,180)
t.pd()
t.fd(80)

#竖
t.penup()
t.goto(40,230)
t.pd()
t.rt(90)
t.fd(50)
#左撇
t.penup()
t.goto(-30,110)
t.pd()
t.rt(45)
t.fd(30)
#竖
t.penup()
t.goto(-10,120)
t.pd()
t.lt(45)
t.fd(30)
#弧
t.penup()
t.goto(-10,90)
t.pd()
t.circle(30,90)
#横
t.penup()
t.goto(20,60)
t.pd()
t.fd(70)
#勾
t.penup()
t.goto(90,60)
t.pd()
t.lt(105)
t.fd(10)
#点
t.penup()
t.goto(40,100)
t.pd()
t.rt(180)
t.fd(10)
#右撇
t.penup()
t.goto(100,110)
t.pd()
t.lt(30)
t.fd(30)
#第三个字
#点
t.penup()
t.goto(240,240)
t.pd()
t.fd(10)
#横
t.penup()
t.goto(160,210)
t.pd()
t.lt(45)
t.fd(160)
#撇
t.penup()
t.goto(200,210)
t.pd()
t.rt(60)
t.fd(180)
#撇
t.penup()
t.goto(280,210)
t.pd()
t.rt(60)
t.fd(180)
t.done()

python中常用的数据类型

列表: 与c语言中的数组很相似, 只不过可以存储不同类型的数据

1. 优缺点

优点:灵活 ,缺点: 效率低

2. 定义方式

列表格式[] 例如:student = ['xmx','wj','yys','chy','ty']
遍历
for hero in student:
print(hero)
xmx wj yys chy ty
常见操作

列表访问:列表名[索引]

print(student[2])

添加

 append()   student('wz')

修改

 student[1] = xxx

删除

del   del student[1]

本地文件读写

1. 文件读取

使用open内置函数进行读取

with open("xmx.txt","r",encoding="utf-8") as f:
data = f.read()
print(data)
使用open内置函数进行写
txt = 'xmx666'
with open('python.txt','w', encoding='utf-8') as f:
f.write(txt)

结巴分词

默认模式,试图将句子最精确地切开,适合文本分析

全模式,把句子中所有的可以成词的词语都扫描出来,适合搜索引擎

中文分词 jieba

PyCharm IDE中,可以直接引入各种工具包。jieba中文分词工具包安装非常方便。
1、打开Pycharm,点击左上角 >>File >>Settings。
2、在settings界面中点击Project :(项目名称) >>Project interpreter 。或者在左上角搜索框里输入“project interpreter”搜索定位。
3、>>点击右绿色“+”号,添加Package。
4、在可用包界面中,输入"jieba"搜索,找到jieba,点击下方“Install …”安装。
5、验证是否安装成功。再次打开Project interpreter( File >>Settings>>Project :
(项目名称) >> Project interpreter),看到列表中已有Package“jieba”。表明安装成功。

安装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 = "我来到北京清华大学"
# 精确模式  精确分词
seg_list = jieba.lcut(seg)
print(seg_list)
# 全模式  找出所有可能的分词结果    冗余性大
seg_list1 = jieba.lcut(seg,cut_all=True)
print(seg_list1)
#  搜索引擎模式
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

import jieba
# 三国演义小说分词
# 读取三国演义小说
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)


词云的展示


安装
pip install wordcloud
本地安装python库

# 导入词云 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')
Python学习的第二天_第1张图片
image.png

你可能感兴趣的:(Python学习的第二天)