Python学习第三天

三国演义人物分析

import jieba
from wordcloud import WordCloud
import imageio
from  matplotlib import  pyplot as plt
from  random import randint
import string
import  numpy as np
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 1.读取小说内容
mask = imageio.imread('./china.jpg')
with open('novel/threekingdom.txt','r',encoding='utf-8') as f:
    words=f.read()

    counts={}  #{'曹操':234,'回寨':56}
    excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
                "如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
                "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
                        '孔明曰','玄德曰','刘备','云长'}
    # 2.分词
    words_list=jieba.lcut(words)
    for word in words_list:
        if len(word)<=1:
            continue
        else:
            #更新字典中的词
            # counts[word]=取出字典中对应的值+1
            # counts[word]=counts[word]+1  #counts[word]如果没有就要报错
            # 字典.get(k)  如果字典中没有这个键,返回是none
            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)

    li=[]  #['孔明','孔明'....]
    ui={}
    for i in range(10):
        # 序列解包
        role,count=items[i]
        print( role,count)
        ui[role]=count
  
        #_是告诉看代码的人,循环里面不需要临时变量
        for _ in range(count):
            li.append((role))
    print(ui)
    lab=ui.keys()
    cou=ui.values()
#饼图展示
    print(lab)
    print(cou)
    plt.pie(cou, labels=lab, shadow=True,autopct='%1.1f%%')
    plt.legend(loc=2)
    plt.axis('equal')
    plt.show()

    text=' '.join(li)

    WordCloud(
        font_path='msyh.ttc',
        background_color='white',
        width=800,
        height=600,
        # 相邻两个重复词之间的匹配
        collocations=False,
        mask=mask
    ).generate(text).to_file('TOP10.png')

Python学习第三天_第1张图片
饼图

Python学习第三天_第2张图片
词云

匿名函数 lambda

结构:

lambda x1,x2....xn:表达式

sum_num = lambda  x1,x2: x1+x2
print(sum_num(2,3))
# 参数可以是无限多个,但是表达式只有一个
name_info_list=[
    ('张三',4500),
    ('李四',9900),
    ('王五',2000),
    ('赵六',5500),
]
name_info_list.sort(key=lambda x:x[1],reverse=True)
print(name_info_list)


stu_info =[
    {"name":'zhangsan',"age":18},
    {"name":'lisi',"age":30},
    {"name":'wangwu',"age":99},
    {"name":'tiaqi',"age":8},
]
stu_info.sort(key=lambda i:i['age'])
print(stu_info)

列表推导式

# 普通for循环
li=[]
for i in range(10):
    li.append(i)
print(li)
 # 列表推导式
 # [for 临时变量 in 可迭代对象 可以追加条件]
print([i for i in range(10)])
  • 列表解析
#筛选出列表中的所有偶数
li=[]
for i in range(10):
    if i%2 ==0:
        li.append(i)
print(li)

# 使用列表解析
print([i for i in range(10) if i%2==0])
  • 筛选出列表中大于0 的数
from random import randint
# 生成随机数
num_list=[randint(-10,10) for _ in range(10)]
print(num_list)
# 筛选大于0的数
print([i for i in num_list if i>0])

字典解析

stu_grades={'student{}'.format(i):randint(50,100)for i in range(1,101)}
print(stu_grades)
# 筛选大于60分的
print({k:v for k,v in stu_grades.items() if v>60})

matplotlib

  • 导入曲线图plt.plot(x,y)
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='--',label='sin(x)')
plt.plot(x,cosy,color='r',label='cos(x)')
plt.xlabel('时间(s)')
plt.ylabel('电压(v)')
plt.title('欢迎来到python世界')
# 图例
plt.legend()
plt.show()
Python学习第三天_第3张图片
曲线图
  • 柱状图plt.bar(x,y)
import  string
from  random import randint
print(string.ascii_uppercase[0:6])
x=['口红{}'.format(x) 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.plot(x,y)
plt.show()

[图片上传中...(image.png-b81f00-1564484037887-0)]

Python学习第三天_第4张图片
柱状图
  • 饼图pit.pie()
from  random import randint
import string
counts = [randint(3500,9000) for _ in range(6)]
labels=['员工{}'.format(x) for x in string.ascii_uppercase[:6]]
# 距离圆心点的距离
explode=[0.1,0,0,0,0,0]
color=['r','purple','b','y','gray','green']
plt.pie(counts,labels=labels,explode=explode,shadow=True,colors=color, autopct='%1.1f%%')
plt.legend(loc=2)
plt.axis('equal')
plt.show()
Python学习第三天_第5张图片
饼图
  • 散点图plt.scatter(x,y)
# 均值为0 标准差为1 的正态分布数据

x=np.random.normal(0,1,1000000)
y=np.random.normal(0,1,1000000)
# alpha 透明度
plt.scatter(x,y,alpha=0.1)
plt.show()
Python学习第三天_第6张图片
散点图

红楼梦人物分析

import jieba
from wordcloud import WordCloud
import imageio
from  matplotlib import  pyplot as plt
from  random import randint
import string
import  numpy as np
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 1.读取小说内容
# mask = imageio.imread('./china.jpg')
with open('novel/all.txt','r',encoding='utf-8') as f:
    words=f.read()

    counts={}  #{'曹操':234,'回寨':56}
    excludes = {'什么','一个','我们','你们','如今','出来','众人','那里','奶奶','太太','一面',
                '只见','知道','姑娘','起来','两个','这里','没有','怎么','不是','不知','这个',
                '听见','这样','进来','咱们','就是','东西','平儿','告诉','袭人','回来','只是',
                '大家','只得','丫头','这些','老爷','他们','不敢','出去','自己','所以','老太太'
                ,'说道','不过','不好','姐姐','老太太'}
    # 2.分词
    words_list=jieba.lcut(words)
    for word in words_list:
        if len(word)<=1:
            continue
        else:
            #更新字典中的词
            # counts[word]=取出字典中对应的值+1
            # counts[word]=counts[word]+1  #counts[word]如果没有就要报错
            # 字典.get(k)  如果字典中没有这个键,返回是none
            counts[word]=counts.get(word,0) + 1
    print(counts)
    # 3.词语过滤,删除无关词,重复词

    for word in excludes:
        del counts[word]


    # 4.排序[(),()]
    # 字典转换成列表
    items=list(counts.items())
    print(items)
    items.sort(key=lambda x:x[1],reverse=True)

    li=[]  #['孔明','孔明'....]
    ui={}
    for i in range(10):
        # 序列解包
        role,count=items[i]
        print( role,count)
        ui[role]=count

        #_是告诉看代码的人,循环里面不需要临时变量
        for _ in range(count):
            li.append((role))
    print(ui)
    lab=ui.keys()
    cou=ui.values()
    print(lab)
    print(cou)
    plt.pie(cou, labels=lab, shadow=True,autopct='%1.1f%%')
    plt.legend(loc=2)
    plt.axis('equal')
    plt.show()

    text=' '.join(li)

    WordCloud(
        font_path='msyh.ttc',
        background_color='white',
        width=800,
        height=600,
        # 相邻两个重复词之间的匹配
        collocations=False,
        # mask=mask
    ).generate(text).to_file('TOP11.png')

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