使用pickle模块序列化数据,优化代码
Python 碎片
共 6711字,需浏览 14分钟
·
2021-05-26 15:58
一、pickle模块介绍
二、pickle可以序列化哪些Python对象
None、True和False
整数、浮点数、复数
str、byte、bytearray
只包含可序列化对象的集合,包括tuple、list、set和dict
定义在模块最外层的函数(使用def定义,lambda函数不可以)
定义在模块最外层的内置函数
定义在模块最外层的类
某些类实例
三、案例分享
1. 将数据序列化保存
# coding=utf-8
import pickle
data = {
... # 文末附完整数据获取方式
}
with open('S10.pkl', 'wb') as pkl_file:
pickle.dump(data, pkl_file)
2. 读取数据并反序列化
# coding=utf-8
import matplotlib.pyplot as plt
from matplotlib import ticker
from numpy import mean
import pickle
with open('S10.pkl', 'rb') as pkl_file:
data = pickle.load(pkl_file)
location = ["上单", "打野", "中单", "下路", "辅助"]
win_loc_kill, win_loc_die, win_loc_assists = [[list() for _ in range(5)] for _ in range(3)]
lose_loc_kill, lose_loc_die, lose_loc_assists = [[list() for _ in range(5)] for _ in range(3)]
for i in range(5):
win_loc_kill[i] = [value[0][i][0] for value in data.values()]
win_loc_die[i] = [value[0][i][1] for value in data.values()]
win_loc_assists[i] = [value[0][i][2] for value in data.values()]
lose_loc_kill[i] = [value[1][i][0] for value in data.values()]
lose_loc_die[i] = [value[1][i][1] for value in data.values()]
lose_loc_assists[i] = [value[1][i][2] for value in data.values()]
# noinspection PyTypeChecker
win_avg_kill = [round(mean(kill), 2) for kill in win_loc_kill]
# noinspection PyTypeChecker
win_avg_die = [round(mean(die), 2) for die in win_loc_die]
# noinspection PyTypeChecker
win_avg_assists = [round(mean(assists), 2) for assists in win_loc_assists]
# noinspection PyTypeChecker
lose_avg_kill = [round(mean(kill), 2) for kill in lose_loc_kill]
# noinspection PyTypeChecker
lose_avg_die = [round(mean(die), 2) for die in lose_loc_die]
# noinspection PyTypeChecker
lose_avg_assists = [round(mean(assists), 2) for assists in lose_loc_assists]
fig, axs = plt.subplots(nrows=2, ncols=1, figsize=(20, 16), dpi=100)
x = range(len(location))
axs[0].bar([i-0.2 for i in x], win_avg_kill, width=0.2, color='b')
axs[0].bar(x, win_avg_die, width=0.2, color='r')
axs[0].bar([i+0.2 for i in x], win_avg_assists, width=0.2, color='g')
axs[1].bar([i-0.2 for i in x], lose_avg_kill, width=0.2, color='b')
axs[1].bar(x, lose_avg_die, width=0.2, color='r')
axs[1].bar([i+0.2 for i in x], lose_avg_assists, width=0.2, color='g')
for a, b in zip(x, win_avg_kill):
axs[0].text(a-0.2, b+0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, win_avg_die):
axs[0].text(a, b+0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, win_avg_assists):
axs[0].text(a+0.2, b+0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, lose_avg_kill):
axs[1].text(a-0.2, b+0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, lose_avg_die):
axs[1].text(a, b+0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for a, b in zip(x, lose_avg_assists):
axs[1].text(a+0.2, b+0.1, '%.02f' % b, ha='center', va='bottom', fontsize=14)
for i in range(2):
axs[i].xaxis.set_major_locator(ticker.FixedLocator(x))
axs[i].xaxis.set_major_formatter(ticker.FixedFormatter(location))
axs[i].set_yticks(range(0, 11, 2))
axs[i].grid(linestyle="--", alpha=0.5)
axs[i].legend(['击杀', '死亡', '助攻'], loc='upper left', fontsize=16, markerscale=0.5)
axs[i].set_xlabel("位置", fontsize=18)
axs[i].set_ylabel("场均数据", fontsize=18, rotation=0)
axs[0].set_title("S10总决赛胜方各位置场均数据", fontsize=18)
axs[1].set_title("S10总决赛负方各位置场均数据", fontsize=18)
plt.show()
评论