匿名函数
def two_sum(x, y):
return x + y
print(two_sum(10, 20))
# lambda 参数1, 参数2, : 表达式
sums = lambda x, y: x+y
print(sums(10, 20))
# 只用一次, 函数的 实现不是很复杂的时候会使用lambda表达式
res_ls = sorted(score_ls, key=lambda t:t[1])
print(res_ls)
第二种常见的复杂类型排序
#复杂类型排序 2
# {} --- > [(), (), ()]
# 2 [{}, {}, {}]
stus_info = [
{"name": "张三", "score":90},
{"name": "李四", "score" :60},
{"name": "王五", "score": 33},
{"name": "赵六", "score": 100},
{"name": "田七", "score": 3},
]
print(stus_info)
stus_info = sorted(stus_info, key=lambda item: item["score"])
print(stus_info)
数据可视化
# 导入matplotlib库 as是起别名
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False #
import string
from random import randint
scores = [randint(500, 750) for _ in range(7)]
print(scores)
stus = ["student{}".format(i) for i in string.ascii_uppercase[:7]]
print(stus)
# 柱状图
plt.bar(stus, scores)
plt.xlabel("student name")
plt.ylabel("student score")
plt.title("top 7 score show", fontsize = 30)
plt.savefig("bar.png")
plt.show()
# 饼图
plt.figure(figsize=(10, 8))
plt.pie(scores, labels=stus, shadow=True, autopct="%1.1f%%")
plt.title("成绩占比", fontsize=35)
plt.legend(loc=4)
plt.savefig("pie.png")
plt.show()
最终代码
import jieba
from wordcloud import WordCloud
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False #
# 读取小说文件
def parse():
with open('threekingdom.txt', mode='r', encoding='utf-8') as f:
data = f.read()
print(type(data))
print(len(data))
# 词频统计 中文单词之间没有空格 , 借助中文分词工具
word_list = jieba.lcut(data)
print(len(word_list))
print(word_list)
text = " ".join(word_list)
wc = WordCloud(
font_path="MSYH.TTC",
background_color="white",
width=500,
height=500
)
wc.generate(text).to_file('三国演义词云.png')
# {"九日":1, "潼关":3....}
counts = {}
for word in word_list:
if len(word) <= 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
print(counts)
# 在前十的排名中要保证没有非人名
stop_words = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
"如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
"东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
"孔明曰","玄德曰", "刘备"}
counts["孔明"] = counts["孔明"] + counts["孔明曰"]
counts["云长"] = counts["云长"] + counts["关公"]
counts["玄德"] = counts["玄德"] + counts["玄德曰"] + counts["刘备"]
for word in stop_words:
del counts[word]
# 重复指代同一个人的需要进行合并
# 排序
word_list = list(counts.items())
print(word_list)
word_list = sorted(word_list, key=lambda x:x[1], reverse=True)
# top10_info = []
top10_info = {}
# 打印出现次数最多的前20个
for i in range(10):
role, nums = word_list[i]
# top10_info.append(
# {"role":role, "nums":nums}
# )
top10_info[role] = nums
print(top10_info)
nums_list = list(top10_info.values())
label_list = list(top10_info.keys())
plt.pie(nums_list, labels=label_list, shadow=True, autopct="%1.1f%%")
plt.title("TOP10出场占比", fontsize=35)
plt.legend(loc=4)
plt.savefig("三国top10.png")
plt.show()
parse()