Python学习的第三天

import jieba
from wordcloud import WordCloud
import imageio
# 1.读取小说内容
with open('./novel/threekingdom.txt', 'r', encoding='utf-8') as f:
    words = f.read()
    counts = {} # counts = {'姓名':出现频率}
    excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
                "如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
                "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
                "孔明曰", "玄德曰", "刘备", "云长"}
    # 2.分词
    words_list = jieba.lcut(words)
    print(words_list)
    for word in words_list:
            if len(word) <= 1:
                continue
            else:
                # 更新字典中的值
                # counts[word] = 取出字典中原来键对应的值 + 1
                # counts[word] = counts[word] + 1
                # 字典.get(k)  如果字典中没有这个键 ,(返回NONE)添加一个默认值:0
                counts[word] = counts.get(word, 0) + 1
    print(counts)
    # 3.词语过滤,删除无关词,重复词
    counts['孔明'] = counts['孔明'] + counts['孔明曰']
    counts['玄德'] = counts['玄德'] + counts['玄德曰'] + counts['刘备']
    counts['关公'] = counts['关公'] + counts['云长']
    for word in excludes:
        del counts[word]
    # 4.排序[(), ()]
    items = list(counts.items())
    print(items)

    # def sort_by_count(x):
    #     return x[1]
    # items.sort(key=sort_by_count, reverse=True)
    # 用列表解析排序
    items.sort(key=lambda x: x[1], reverse=True)
    # print(items)
    li = []  # ['孔明', '孔明', '孔明',..., '曹操'...]
    for i in range(10):
        # 序列解包
        role, count = items[i]
        print(role, count)
        # _ 是告诉看代码的人,循环里面不需要使用临时变量
        for _ in range(count):
            li.append(role)
        # 得出结论
        # 绘制中文词云,在WordCloud()里面设置参数
        text = ' '.join(li)
        wc = WordCloud(
            font_path='msyh.ttc',
            background_color='white',
            width=800,
            height=600,
            # 相邻两个重复词之间的匹配,关掉
            collocations=False
        ).generate(text)
        wc.to_file('三国TOP10人物词云.png')
三国TOP10人物词云.png

2、匿名函数 lambda

结构: lambda x1, x2, ...xn: 表达式
eg:

sum_num = lambda x1, x2: x1+x2
print(sum_num(2, 3))
# 结果:5

参数可以是无限多个,但是表达式只有一个
eg1:从大到小排序

name_info_list = [
    ('张三', 4500),
    ('李四', 9500),
    ('王五', 2000),
    ('赵六', 5500),
]
name_info_list.sort(key=lambda x: x[1], reverse=True)
print(name_info_list)
# 结果:[('李四', 9500), ('赵六', 5500), ('张三', 4500), ('王五', 2000)]

eg2:从小到大排序

stu_info = [
    {"name": 'zhangsan', "age": 18},
    {"name": 'lisi', "age": 30},
    {"name": 'wangwu', "age": 99},
    {"name": 'tianqi', "age": 3},
]
stu_info.sort(key=lambda i: i['age'])
print(stu_info)
# 结果:[{'name': 'tianqi', 'age': 3}, {'name': 'zhangsan', 'age': 18}, {'name': 'lisi', 'age': 30}, {'name': 'wangwu', 'age': 99}]

3、列表推导式,列表解析和字典解析

1、列表推导式

之前用普通for 创建列表

li = []
for i in range(10):
    li.append(i)
print(li)
# 结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

使用列表推导式创建列表
结构:[表达式 for 临时变量 in 可迭代对象 可以追加条件]

print([i for i in range(10)])
# 结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2、 列表解析

普通筛选出列表中所有的偶数

# 筛选出列表中所有的偶数
li = []
for i in range(10):
    if i % 2 == 0:
        li.append(i)
print(li)
# 结果:[0, 2, 4, 6, 8]

使用列表解析筛选偶数

print([i for i in range(10) if i % 2 == 0])
# 结果:[0, 2, 4, 6, 8]

筛选出列表中 大于0 的数

# 随机生成(-10, 10)的10个数
from random import randint
num_list = [randint(-10, 10) for _ in range(10)]
print(num_list)
# 输出num_list中 大于0 的数
print([i for i in num_list if i > 0])
# 结果:[-5, -1, -4, -8, -8, -1, -8, 7, 10, -4]
#      [7, 10]

4、Matplotlib

导入

from matplotlib import pyplot as plt

使用100个点 绘制[0, 2π]正余弦曲线图

from matplotlib import pyplot as plt
import numpy as np
# 处理中文乱码
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 使用100个点  绘制[0, 2π]正弦曲线图
# .linspace 左闭右闭区间的等差数列
x = np.linspace(0, 2*np.pi, num=100)
print(x)
y = np.sin(x)
# 正弦和余弦在同一坐标系下
cosy = np.cos(x)
plt.plot(x, y, color='g', linestyle='--')
plt.plot(x, cosy, color='r')
plt.xlabel('时间(s)')
plt.ylabel('电压(v)')
plt.title('欢迎来到python世界')
# 图例
plt.legend()
plt.show()

结果:


image.png

柱状图

# 切片
# print(string.ascii_uppercase[0: 6])
# 结果:ABCDEF

# 柱状图
from matplotlib import pyplot as plt
# 处理中文乱码
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['口红{}'.format() for x in string.ascii_uppercase[:5]]
y = [randint(200, 500) for _ in range(5)]
print(x)
print(y)
plt.xlabel('口红品牌')
plt.ylabel('价格(元)')
plt.bar(x, y)
plt.show()

结果:


image.png

饼图

# 饼图
from matplotlib import pyplot as plt
# 处理中文乱码
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
from random import randint
counts = [randint(3500, 9000) for _ in range(6)]
labels = ['员工{}'.format(x) for x in string.ascii_lowercase[:6]]
# 距离圆心点距离
explode = [0.1, 0, 0, 0, 0, 0]
color = ['red', 'purple', 'blue', 'yellow', 'gray', 'green']
plt.pie(counts, explode=explode, shadow=True, labels=labels, autopct='%1.1f%%', colors=color)
plt.axis('equal')
plt.legend(loc=2)
plt.show()

结果:


image.png

散点图

from matplotlib import pyplot as plt
import numpy as np
# 均值为 0 标准差为 1 的正态分布数据
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
plt.scatter(x, y)
plt.show()

结果:


image.png

有透明度的散点图

from matplotlib import pyplot as plt
import numpy as np
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
# alpha透明度
plt.scatter(x, y, alpha=0.1)
plt.show()

结果:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、绘制词云 导入包 读取小说内容 分词 词语过滤,删除无关词,重复词 排序 序列解包 结论 二、匿名函数 匿名函...
    喵青禾阅读 2,563评论 0 0
  • 三国TOP10人物分析 运行结果: 匿名函数 ①结构:lambda x1,x2...xn:表达式 注:参数可以是无...
    Amieee阅读 2,750评论 0 0
  • 1. 词云WordCloud——续 ①Python中使用open内置函数进行文件读取②利用函数jieba.lcut...
    婉儿吖阅读 2,999评论 0 0
  • 1.三国演义Top10人物分析 效果图 2.匿名函数(lambda) lambda x1, x2, ...xn: ...
    神坑少女7阅读 1,144评论 0 0
  • 三国演义Top10 人物词云绘制 红楼梦 Top10 人物词云绘制 匿名函数 结构: lambda x1, x2,...
    可可西里_4160阅读 2,669评论 0 0