10月15日学习总结
"""
example01 - 定义列表
方法一: 继承list类,再添加我们需要用到的方法
debug ---> 调试 ---> 解决缺陷(问题)
mobilize ---> demobilize
encode ---> decode
moden ---> moderate / demoderate ---> 调制解调器 ---> 猫
我们可以通过继承实现对已有代码的复用,也可以通过委派来实现对已有代码的复用
"""
class Mylist(list):
def __init__(self, *args):
super().__init__()
for value in args:
self.append(value)
def append(self,value):
if not isinstance(value,(int,float)):
raise ValueError('只能添加int或float类型的元素')
super().append(value)
def insert(self, index, value):
if not isinstance(value,(int,float)):
raise ValueError('只能添加int或float类型的元素')
super().insert(index, value)
def mean(self):
"""均值"""
return sum(self) / len(self)
def median(self):
"""中位数"""
length = len(self)
sorted_list = sorted(self)
if length % 2 != 0:
return sorted_list[length // 2]
return (sorted_list[length // 2 - 1] + sorted_list[length // 2]) / 2
def std(self):
"""标准差(standard deviation)"""
return self.var() ** 0.5
def var(self):
"""样本方差"""
x_bar = self.mean()
return sum([(x - x_bar) ** 2 for x in self]) / (len(self) - 1)
nums = Mylist(1,2,3,4,5)
print(nums)
nums.append(100)
nums.insert(0,200)
nums.remove(4)
print(nums)
nums += [8,12,15]
print(sorted(nums))
print('均值:',nums.mean())
print('中位数:',nums.median())
print('方差:',nums.var())
print('标准差:',nums.std())
二. 三方库的应用
1. PyQt5
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
def close_window():
reply = QMessageBox.question(window, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
app.quit()
app = QApplication(sys.argv)
window = QWidget()
window.resize(1000, 800)
window.setWindowTitle('My First App')
button = QPushButton('Hit Me!', window)
button.setToolTip('这是一个按钮')
button.resize(100, 30)
button.move(100, 100)
button.clicked.connect(close_window)
window.show()
sys.exit(app.exec())
2. pyecharts
"""
example03 使用pyecharts
"""
柱状图
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
bar.add_yaxis("商家B", [18, 27, 30, 167, 79, 96])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render('sales.html')
zip函数
keys = ['A','B','C','D','E']
values = (20,30,40,50,60)
elems = ('Apple', 'Banana', 'Grape')
# my_dict = dict(zip(keys, values))
# print(my_dict)
for elem in zip(keys, values, elems):
print(elem)
# ('A', 20, 'Apple')
# ('B', 30, 'Banana')
# ('C', 40, 'Grape')
# 饼状图
import pyecharts.options as opts
from pyecharts.charts import Pie
x_data = ["直接访问", "邮件营销", "联盟广告", "视频广告", "搜索引擎"]
y_data = [335, 310, 274, 235, 400]
data_pair = [list(z) for z in zip(x_data, y_data)]
data_pair.sort(key=lambda x: x[1])
print(data_pair)
(
Pie(init_opts=opts.InitOpts(width="1600px", height="800px", bg_color="#2c343c"))
.add(
series_name="访问来源",
data_pair=data_pair,
rosetype="radius",
radius="55%",
center=["50%", "50%"],
label_opts=opts.LabelOpts(is_show=False, position="center"),
)
.set_global_opts(
title_opts=opts.TitleOpts(
title="Customized Pie",
pos_left="center",
pos_top="20",
title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
),
legend_opts=opts.LegendOpts(is_show=False),
)
.set_series_opts(
tooltip_opts=opts.TooltipOpts(
trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
),
label_opts=opts.LabelOpts(color="rgba(255, 255, 255, 0.3)"),
)
.render("customized_pie.html")
)