一、lambda表达式——匿名函数
- 定义:lambda x1, x2....xn: 表达式
- 特点:参数可以是无限多个,但是表达式只有一个,即返回值
- 应用例子
#应用于只使用一次的函数,如排序时
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":3},
]
stu_info.sort(key=lambda i:i['age'])
print(stu_info)
二、列表推导式
- 定义:[表达式 for 临时变量 in 可迭代对象 可以追加条件]
- 使用例子
#筛选出偶数
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)]#在-10和10之间生成10个随机数
print(num_list)
print([i for i in num_list if i>0])
# 筛选大于 60分的所有学生
stu_grades = {'student{}'.format(i):randint(50, 100) for i in range(1, 101)}# 生成100个学生的成绩
print({k: v for k, v in stu_grades.items() if v >60})
三、使用matplotlib绘图库进行图表绘制
折线图——plot
#使用100个点绘制[0,2π]正弦曲线图
x = np.linspace(0,2*np.pi,num=100)#linspace() 左闭右闭区间的等差数列
print(x)
siny = np.sin(x)
cosy = np.cos(x)
plt.plot(x,siny,color='r',label='sin(x)',linestyle=':')
plt.plot(x,cosy,color='b',label='cos(x)')
plt.xlabel('时间/s')
plt.ylabel('电压/V')
plt.title('welcome')
plt.legend() #图例
plt.show()
柱状图——bar
import string
from random import randint
x = ['口红{}'.format(x) for x in string.ascii_letters[:5]]
y = [randint(200,500) for _ in range(5)]
print(x)
print(y)
plt.xlabel('品牌')
plt.ylabel('销量')
plt.title('销量表')
plt.bar(x,y)
plt.show()
饼图——pie
from random import randint
import string
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]
colors = ['red', 'purple','blue', 'yellow','gray','green']
plt.pie(counts,explode = explode,shadow=True, labels=labels, autopct = '%1.1f%%',colors=colors)
plt.legend(loc=2)#loc参数指定图例位于第几象限
plt.axis('equal')#使图例和图不重叠
plt.show()
散点图——scatter
均值为 0 标准差为1 的正态分布数据
x = np.random.normal(0, 1, 1000)
y = np.random.normal(0, 1, 1000)
plt.scatter(x, y, alpha=0.1)
plt.show()
四、文本分析
三国演义人名词频分析
import jieba
from wordcloud import WordCloud
import imageio
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
import numpy as np
with open("./novel/threekingdom.txt", "r", encoding="UTF-8") as f:
words = f.read()
counts = {}
word_list = jieba.lcut(words)
for word in word_list:
if len(word)<=1:
continue
else:
counts[word] = counts.get(word, 0) + 1
print(counts)
counts["孔明"] = counts["孔明"] + counts["孔明曰"]
counts["玄德"] = counts["玄德"] + counts["玄德曰"] + counts["刘备"]
counts["关公"] = counts["关公"] + counts["云长"]
excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
"如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
"东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
"孔明曰", "玄德曰", "刘备", "云长"}
for word in excludes:
del counts[word]
#将字典转化成元组型列表
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
#序列解包
li = []
for i in range(10):
role, count = items[i]
print(role, count)
for _ in range(count):#_表示循环里面用不到这个临时变量
li.append(role)
text = " ".join(li)
WordCloud(
font_path = "msyh.ttc",
background_color = 'white',
width = 800,
height = 600,
collocations = False,
mask = imageio.imread('./china.jpg')
).generate(text).to_file("TOP10.jpg")
#将词频前十的人名生成饼图
name = []
times = []
for i in range(0,10):
name.append(items[i][0])
times.append(items[i][1])
print(name)
print(times)
plt.pie(times, explode=[0.3,0,0,0,0,0,0,0,0,0], shadow=True, labels=name, autopct='%1.1f%%')
plt.legend(loc=2)#loc参数指定图例位于第几象限
plt.axis('equal')#使图例和图不重叠
plt.show()
红楼梦人名词频分析
#找出人名词频前13的人物
import jieba
from wordcloud import WordCloud
import imageio
from matplotlib import pyplot as plt
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
import numpy as np
with open("./novel/dreamoftheredchamber.txt", "r", encoding="UTF-8") as f:
words = f.read()
counts = {}
word_list = jieba.lcut(words)
for word in word_list:
if len(word)<=1:
continue
else:
counts[word] = counts.get(word, 0) + 1
counts["贾母"] = counts["贾母"] + counts["老太太"]
counts["贾政"] = counts["贾政"] + counts["老爷"]
counts["凤姐"] = counts["凤姐"] + counts["凤姐儿"] + counts["王熙凤"]
counts['黛玉'] = counts['黛玉'] + counts['林黛玉']
counts['宝玉'] = counts['宝玉'] + counts['贾宝玉']
counts['宝钗'] = counts['宝钗'] + counts['薛宝钗']
counts['王夫人'] = counts['王夫人'] + counts['太太']
excludes = {"老太太", "什么", "一个", "我们", "你们", "如今", "说道", "知道", "姑娘",
"起来", "这里", "出来", "众人", "那里", "奶奶", "自己", "太太", "只见",
"两个", "没有", "怎么", "一面", "不是", "不知", "这个", "听见", "这样",
"进来", "咱们", "就是", "东西", "告诉", "回来", "只是", "大家", "老爷",
"只得", "丫头", "他们", "不敢", "出去", "这些", "所以", "不过", "不好",
"姐姐", "凤姐儿", "的话", "一时", "王熙凤", "薛宝钗", "林黛玉", "贾宝玉"}
for word in excludes:
del counts[word]
items = list(counts.items())#将字典转化成元组型列表
items.sort(key=lambda x:x[1], reverse=True)
print(items)
li = []
for i in range(13):
role, count = items[i]
print(role, count)
for _ in range(count):#_表示循环里面用不到这个临时变量
li.append(role)
text = " ".join(li)
WordCloud(
font_path = "msyh.ttc",
background_color = 'white',
width = 800,
height = 600,
collocations = False,
mask = imageio.imread('./china.jpg')
).generate(text).to_file("TOP13.jpg")