def

面向对象-单继承、多继承、菱形继承

喜欢而已 提交于 2021-01-12 20:24:16
单继承与多继承 单继承: 一个类只能继承一个父类的方式。 多继承 :一个类可以继承多个父类的方式。 单继承: (生物角度) 人类->哺乳类动物->动物->生物->有机物.... 多继承: (社会角度) 舞蹈老师(教舞蹈) 体育老师(运动) 爸爸(抽烟) 妈妈(打扫卫生) 我(舞蹈,运动,抽烟,打扫卫生) 单继承案例: 父类: pass 子类(父类): pass 多继承案例: 父类1: pass 父类2: pass 父类3: pass 子类(父类1,父类2,父类3): pass 多继承的问题所在:菱形继承或者钻石继承中的问题 。 菱形继承的bug解决: MRO列表和super 类 当我们定义一个菱形继承关系的时候,程序会自动生成一个新的MRO列表。 MRO列表: Method Realtion Order 方法关系列表。 MRO列表的生成采用了C3算法完成的。 C3算法的原则: 1.子类永远在父类的前面2.同一等级的类,按照子类中的继承顺序摆放 super()调用的时候,不是查找父类!!!! 实际上super是查找MRO列表的上一个类 super()调用对象方法的时候不需要传入对象,自动传入 补充: 如果需要查看mro列表,可以使用对象.mro() 方法查看(非必要~) 1.单继承案例 1 # 单继承案例 2 ''' 3 爷爷类 4 爸爸类 5 儿子类 6 ''' 7 8 # 爷爷类

【leetcode】1146. Snapshot Array

♀尐吖头ヾ 提交于 2021-01-12 08:48:28
题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0. void set(index, val) sets the element at the given index to be equal to val . int snap() takes a snapshot of the array and returns the snap_id : the total number of times we called snap() minus 1 . int get(index, snap_id) returns the value at the given index , at the time we took the snapshot with the given snap_id Example 1: Input: ["SnapshotArray","set","snap","set","get"] [[3],[0,5],[],[0,6],[0,0]]

NumPy索引切片

北慕城南 提交于 2021-01-12 08:31:52
索引,切片 和迭代 一维 数组可以被索引,切片和迭代,就像 列表 和其他Python序列一样。 代码实例解析数组中的索引切片 >>> import numpy as np #导入numpy 别名为np >>> a = np. arange( 10) ** 3 >>> a array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) >>> a[ 2] #数组的索引从下标0开始 8 >>> a[ 2: 5] #数组的索引包含首不包含尾 array([ 8, 27, 64]) >>> a[: 6: 2] = - 1000 # 指索引[0:6]步长为2的值都赋值为1000 >>> a array([ - 1000, 1, - 1000, 27, - 1000, 125, 216, 343, 512, 729]) >>> a[ : : - 1] # 将 a 逆序 array([ 729, 512, 343, 216, 125, - 1000, 27, - 1000, 1, - 1000]) >>> for i in a: ... print( i **( 1 / 3.)) ... nan 1.0 nan 3.0 nan 5.0 6.0 7.0 8.0 9.0 多维 数组每个轴可以有一个索引。这些索引以逗号分隔的元组给出: >>> def f( x,

Django 模板语言 标签

我怕爱的太早我们不能终老 提交于 2021-01-12 04:36:48
前言:django的模板语法基本和flask的jinja2基本一样。下面比较一下两个模板语法的区别。 ------ 深度变量的查找(万能的句点号) 在 Django 模板中遍历复杂数据结构的关键是句点字符 ( . )。 1.模板变量 django :{{ 变量 }} # 因为django只有一个context返回,全部数据都集中在一起 jinja2 :{{ 对象.变量 }} 2.根据列表的下标获取值 django :{{ 列表.0 }} jinja2 :{{ 列表[0] }} 3.根据字典的键获取字典的值 django :{{ 字典.key }} jinja2 :{{ 字典[key] }}或者{{ 字典.key }} 4.for循环时取序号 django : {% for item in 列表 %} {{forloop.counter}} < 1-- 表示当前是第几次循环,从 1开始 --> {{forloop.counter0}} <!-- 表示当前是第几次循环,从 0开始 --> {% endfor %} jinja2 : {% for item in 列表 %} {{loop.index}} < 1-- 表示当前是第几次循环,从 1开始 --> {{loop.index0}} <!-- 表示当前是第几次循环,从 0开始 --> {% endfor %} # for遍历字典 {

类连接mysql

旧巷老猫 提交于 2021-01-12 04:21:07
# 引入pymysql模块 import pymysql class DoMysql: # 初始化 def __init__ (self): # 创建连接 self.conn = pymysql.Connect( host = ' localhost ' , port = 3306 , user = ' root ' , password = ' root ' , db = ' testdb ' , charset = ' utf8 ' , cursorclass = pymysql.cursors.DictCursor # 以字典的形式返回数据 ) # 获取游标 self.cursor = self.conn.cursor() # 返回多条数据 def fetchAll(self,sql): self.cursor.execute(sql) return self.cursor.fetchall() # 插入一条数据 def insert_one(self,sql): result = self.cursor.execute(sql) self.conn.commit() return result # 插入多条数据 def insert_many(self,sql,datas): result = self.cursor.executemany(sql,datas) self

python学习算术运算

余生长醉 提交于 2021-01-10 21:24:29
运算重新定义 算数运算符 __add__(self, other) 定义加法的行为:+ __sub__(self, other) 定义减法的行为:- __mul__(self, other) 定义乘法的行为:* __truediv__(self, other) 定义真除法的行为:/ __floordiv__(self, other) 定义整数除法的行为:// __mod__(self, other) 定义取模算法的行为:% __divmod__(self, other) 定义当被 divmod() 调用时的行为 __pow__(self, other[, modulo]) 定义当被 power() 调用或 ** 运算时的行为 __lshift__(self, other) 定义按位左移位的行为:<< __rshift__(self, other) 定义按位右移位的行为:>> __and__(self, other) 定义按位与操作的行为:& __xor__(self, other) 定义按位异或操作的行为:^ __or__(self, other) 定义按位或操作的行为:| 更多 https://fishc.com.cn/forum.php?mod=viewthread&tid=48793 1 >>> class New_int(int): 2 def __add__ (self,

选择排序法&快速排序法

北城余情 提交于 2021-01-10 17:10:37
选择排序法:每次遍历整个数组,选出其中最小值。如果数组长度为n,则需要(n-1)+(n-2)+...+2+1次操作,则用大O表示法表示应该为O(n*n/2),但是大O表示法省略诸如1/2这样的常数,因此该方法的大O表示为O(n^2)。 Python代码: >>> def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1 , len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i return smallest_index >>> def selectionSort(arr): newArr = [] for i in range(len(arr)): smallest = findSmallest(arr) newArr.append(arr.pop(smallest)) return newArr 测试: >>> selectionSort([5,3,6,2,10 ]) [ 2, 3, 5, 6, 10 ] >>> C#代码: namespace Algorithms { public static class SelectionSort { public static List< double >

LeetCode 155:最小栈 Min Stack

岁酱吖の 提交于 2021-01-10 16:55:07
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) -- 将元素 x 推入栈中。 pop() -- 删除栈顶的元素。 top() -- 获取栈顶元素。 getMin() -- 检索栈中的最小元素。 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack

python异常处理

有些话、适合烂在心里 提交于 2021-01-10 15:13:55
day13回顾 模块和包 import 语句 示例: import math import mypack.games.contra math.xxx mypack.games.contra.xxx from import 语句 示例: from math import sin sin() # 不需要加模块名或包路径 from mypack.games.contra import play play() # 多余 from import *语句 同上 sys模块 sys.path sys.version sys.modules sys.argv sys.exit() 退出程序   自定义 xxxx.py 全局变量 ---> 属性 预置属性 __doc__属性(当前模块的文档字符串) __file__属性(绑定路径) __name__属性(用来绑定模块名,区分主模块和非主模块) from import * 语句导入 __all__ = ['aaa', 'bbb'] _xxxx = 1000 # 隐藏属性    随机模块 random 包 __init__.py __all__ 列表一定要在__init__.py 包的导入 绝对导入   from mypack.games.contra import * 相对导入   from .menu import show_menu from .

Python异常处理

孤人 提交于 2021-01-10 15:13:35
异常处理格式: try: 执行业务语句 raise 自定义异常类型() except 自定义异常类型: 处理异常语句 except (异常类型1,异常类型2,……): #多个异常类型分别用逗号隔开 处理异常语句 except Exception: #万能匹配,未知错误类型! 处理异常语句 else: 没有异常才会执行语句 finally: 不管有没异常都会执行语句 简要说明: 1.raise 语句可以自定义报错信息,如上。 2. raise后的语句是不会被执行了,因为已经抛出异常,控制流将会跳到异常捕捉模块。 3. except 语句可以一个except后带多个异常,也可以用多个语句捕捉多个异常,分别做不同处理,如果不清楚具体异常类型,最后可用Exception做万能匹配。 4. except语句捕捉的异常如果没有发生,那么except里的语句块是不被执行的。而是执行else里的语句 5. 在上面语句中try/except/else/finally所出现的顺序必须是try–>except X–>except–>else–>finally,即所有的except必须在else和finally之前,else(如果有的话)必须在finally之前,而except X必须在except之前。否则会出现语法错误。 6.else和finally都是可选的. 7.在上面的完整语句中