完结篇啦!
一个阶段的结束象征着另一个阶段的重新开始,未来还要继续加油呀。
下面贴上最后两章的编程题代码,如有错误欢迎指正,不胜感激。
第九章 python标准库概览
#1 使用turtle绘制一个蜂窝状六边形。
import turtle as t
# 定义一个画六边形的函数
def draw_hexagon(x,y,r):
t.hideturtle() # 隐藏画笔
t.speed(8) # 画笔绘制速度
t.penup() # 起笔
t.goto(x,y) # 到达x,y处开始绘制
t.pendown() # 绘制
t.begin_fill() # 填充
t.color('green','red') # 填充颜色
t.circle(r,steps = 6) # 画一个六边形
t.end_fill() # 填充完成(绿边红底)
r = eval(input("请输入半径:")) # 输入半径
t.setup(800,600,200,200) # 设置窗口
R = pow(3,0.5)*r # 辅助画笔移动
draw_hexagon(0,0,r) # 画笔起点在一个六边形的最下边尖角处
draw_hexagon(-R,0,r)
draw_hexagon(R,0,r)
draw_hexagon(-R/2,r*1.5,r)
draw_hexagon(R/2,r*1.5,r)
draw_hexagon(-R/2,-r*1.5,r)
draw_hexagon(R/2,-r*1.5,r)
t.done() # 绘制完成
输出结果
#2 使用turtle库绘制一朵玫瑰花
import turtle
# 设置初始位置
turtle.speed(10)
turtle.penup()
turtle.left(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
# 花蕊
turtle.fillcolor("red")
turtle.begin_fill() # 填充红色,封闭时填充
turtle.circle(10, 180)
turtle.circle(25, 110)
turtle.left(50)
turtle.circle(60, 45)
turtle.circle(20, 170)
turtle.right(24)
turtle.fd(30)
turtle.left(10)
turtle.circle(30, 110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90, 70)
turtle.circle(30, 150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80, 90)
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turtle.fd(20)
turtle.left(155)
turtle.circle(150, 80)
turtle.left(50)
turtle.circle(150, 90)
turtle.end_fill() # 结束填充
# # 花瓣1
turtle.left(150)
turtle.circle(-90, 70)
turtle.left(20)
turtle.circle(75, 105)
turtle.setheading(60) # 设置当前朝向
turtle.circle(80, 98)
turtle.circle(-90, 40)
# 花瓣2
turtle.left(180)
turtle.circle(90, 40)
turtle.circle(-80, 98)
turtle.setheading(-83)
# 叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80, 90)
turtle.right(90)
turtle.circle(-80, 90)
turtle.end_fill()
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
# 叶子2
turtle.right(90)
turtle.right(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(80, 90)
turtle.left(90)
turtle.circle(80, 90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200, 60)
turtle.done()
# 原文链接:https://blog.csdn.net/zaq0123/article/details/78573186/
输出结果
#3 使用turtle库绘制一个颜色图谱
import turtle
# 初始化设置
turtle.setup(800,550,None,None)
turtle.pensize(8)
turtle.speed(10)
dis = 0 # 作为向下的变量
# 使用for循环来生成颜色图谱
for r in range(0,256,100):
for g in range(0,256,100):
for b in range(0,256,50):
turtle.pencolor(r/255,g/255,b/255)
dis += 8
turtle.penup()
turtle.goto(-300,230-dis)
turtle.pendown() # 开始绘制
turtle.forward(600) # 向前画600个像素
turtle.done()
#4 利用random库生成一个包含有10个0-100之间的随机整数列表
import random
list_num = []
for i in range(10):
str_num = ''
num = random.randint(0,100)
str_num += str(num)
list_num.append(str_num)
print(list_num)
# 输出结果
['8', '69', '51', '29', '80', '12', '68', '75', '25', '10']
#5 利用time库将当前时间转化成类似"Sunday,8.January 2017 11:03PM"的格式
import time
t = time.strftime("%A,%d.%B %Y %I:%M%p".format(time.localtime()))
print(t)
# 输出结果
Monday,27.January 2020 04:34PM
第十章 python第三方库概览
#1 使用jieba.cut()对"python是最有意思的编程语言"进行分词,输出结果,并将该迭代器转化为列表类型
import jieba
txt = 'python是最有意思的编程语言'
new_txt = jieba.cut(txt) # 生成的是一个迭代器对象,加到列表中操作
word_list = [word for word in new_txt]
print(word_list)
# 输出结果
['python', '是', '最', '有意思', '的', '编程语言']
#2 使用jieba.cut()对“今天晚上我吃了意大利面”进行分词,输出结果,并使‘意大利面’作为一个词出现在结果中
import jieba
txt = '今天晚上我吃了意大利面'
jieba.add_word('意大利面')
new_txt = jieba.cut(txt) # 和上题一样(如果使用lcut()会更加简单)
word_list = [word for word in new_txt]
print(word_list)
# 输出结果
['今天', '晚上', '我', '吃', '了', '意大利面']
#3 自选一篇报告或者演讲稿,利用jieba分析其词频排前五的关键词。
import jieba
'''
今天上午一觉起来看到新闻,一代传奇篮球巨星科比因为飞机失事去世
内心五味杂陈,特在网上找了一篇和科比有关的文章
致敬科比
R.I.P
'''
# 读入文件
f = open('有一种精神叫科比.txt','r',encoding='GBK')
txt = f.read()
f.close()
# 除去不需要的字词(可根据情况自己选定)
excludes= {
'一个','因为','对于',
'人们','属于','可以',
'没有','只是','时候','一切'
}
# 分词
words = jieba.lcut(txt)
# 词频统计
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
for word in excludes:
del counts[word]
items = list(counts.items())
items.sort(key = lambda x : x[1],reverse=True)
for i in range(5):
word,count = items[i]
print("{0:<10} {1:>5}".format(word,count))
# 输出结果
科比 38
男人 6
乔丹 5
篮球 5
奥尼尔 4
#4 参考本章最后的实例,选择你最喜欢的小说,统计出场人物词频排名。
'''
此题和书上的代码基本一致,没有什么新的要求
在此把书上的代码重写录入一遍
'''
import jieba
excludes = {"什么","一个","我们","那里","你们","如今",
"说道", "知道", "老太太", "起来", "姑娘", "这里",
"出来", "他们", "众人", "自己", "一面", "太太",
"只见", "怎么", "奶奶", "两个", "没有", "不是",
"不知", "这个", "听见",
}
f = open('红楼梦.txt','r')
txt = f.read()
f.close()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
for word in excludes:
del counts[word]
items = list(counts.items())
items.sort(key = lambda x : x[1],reverse=True)
for i in range(5):
word,count = items[i]
print("{0:<10} {1:>5}".format(word,count))
# 输出结果
宝玉 3748
贾母 1228
凤姐 1100
王夫人 1011
贾琏 670
#5 续上题,将上题结果以词云方式展现,并尝试美化生成的词云图片
import jieba
from wordcloud import WordCloud
excludes = {"什么", "一个", "我们", "那里", "你们", "如今",
"说道", "知道", "老太太", "起来", "姑娘", "这里",
"出来", "他们", "众人", "自己", "一面", "太太",
"只见", "怎么", "奶奶", "两个", "没有", "不是",
"不知", "这个", "听见",
}
f = open('红楼梦.txt', 'r')
txt = f.read()
f.close()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
for word in excludes:
del counts[word]
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
new_txt = ''
for i in range(5):
word, count = items[i]
new_txt += word
wordcloud = WordCloud(
background_color='white',
width=800,
height=600,
font_path='msyh.ttc',
max_words=5,
max_font_size=80,
stopwords=excludes
).generate(new_txt)
wordcloud.to_file('红楼梦基本词云.png')