- 集合支持一系列标准操作。
a = t | s # t与s的并集
b = t & s #t和s的并集
c = t - s # 差集
d = t ^ s # 对称差集(项在t或s中,但不会同时出现在两者中)
集合的更多操作 - 引用与复制
浅复制
在上面这个例子中,a和b是单独的列表对象,但是他们包含的元素是共享的。因此,修改b的一个元素也会修改a中对应的元素。
如果是a = b赋值,a与b完完全全是相同的对象,如果是b = a[:], b = list(a), b = a.copy(), 这三种方法,里面第一层的元素是非公用的,但是里面嵌套的[]是公用的。只有使用copy.deepcopy()元素才是全部非共享
b = a[:]赋值
d = list©赋值
f = e.copy()赋值
深复制
-
对象
列表,字典可以存储对象,函数可以返回函数对象本身,对象类可以动态生成,也可以动态添加函数。
列表存对象
字典存储对象
假定要通过正确的类型转换把一行文本GOOG, 100, 490.10转换为一个字段列表,利用上述知识
返回函数
动态地给class绑定属性和动态地绑定函数
利用type()动态地创建类
-
@property
装饰器
使用@property
可以在类通过像访问属性一样调用函数
-
complex() 函数 (复数)
-
any()
和all()
的使用
上面两个函数可以用来判断一个序列中是否有False的情况
是否全为真
是否有一个为真
-
<< >>运算符
a = 60
二进制为0011 1100
11.python所有内置函数
12.python heapq-模块
13.sort()和sorted()的不同,sort()用在list上,后者用在所有可迭代对象上.参考文章1参考文章2
14.廖雪峰Python高阶函数教程. itertools模块
鸭子类型
monkey patch
猴子补丁ex
python3改进
Python3新增
Python3改进
Python如何传递参数
第一个输出[0], [0, 0], 第二个输出 ’ hehea’, ’ hehea’
输出[1, 1], [1, 1, 1]
什么是Cpython GIL
GIL的影响
如何规避GIL影响
为什么有了GIL还要关注线程安全
通过线程锁来确保安全线程安全
服务器性能优化措施
什么是生成器
基于生成器的协程
廖雪峰协程教程和评论解释
explain,总结就是r = yield 1语句,左边执行完后并返回结果,并且函数进入暂定状态,在下一次使用send() or next()的时候,再赋值给r,函数再继续执行下去。
基于生成器的协程
协程的注意点
协程装饰器
单元测试
单元测试相关的库
单元测试ex
常用内置数据结构和算法
namedtuple()
队列 deque()
计数器 Counter
顺序字典 OrderedDict()
初始化默认值字典 defaultdict()
Python dict 底层结构
哈希冲突
开放地址法
链式地址法
建立公共溢出区 再哈希法
LRUCache?
算法常考点
链表
队列
栈
字典与集合
堆
TopK
栈:
使用两个队列实现栈
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.l1 = []
self.l2 = []
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
if self.l1:
self.l1.append(x)
else:
self.l2.append(x)
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
if self.l1: # 1不空, 2空
# pop to l2
while self.l1:
if len(self.l1) == 1:
return self.l1.pop(0)
else:
self.l2.append(self.l1.pop(0))
else:
while self.l2:
if len(self.l2) == 1:
return self.l2.pop(0)
else:
self.l1.append(self.l2.pop(0))
def top(self) -> int:
"""
Get the top element.
"""
if self.l1: # 1不空, 2空
# pop to l2
while self.l1:
if len(self.l1) == 1:
tmp = self.l1.pop(0)
self.l2.append(tmp)
return tmp
else:
self.l2.append(self.l1.pop(0))
else: # 2不空, 1空
while self.l2:
if len(self.l2) == 1:
tmp = self.l2.pop(0)
self.l1.append(tmp)
return tmp
else:
self.l1.append(self.l2.pop(0))
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
if self.l1 or self.l2:
return False
else:
return True
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
队列
用两个栈实现队列
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.l1 = [] # 进
self.l2 = [] # 出
def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.l1.append(x)
def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
if self.l2: # l2有存
return self.l2.pop()
else:
# l1 -> l2
while self.l1:
self.l2.append(self.l1.pop())
# l1:[],
return self.l2.pop()
def peek(self) -> int:
"""
Get the front element.
"""
if self.l2: # l2有存
return self.l2[-1]
else:
# l1 -> l2
while self.l1:
self.l2.append(self.l1.pop())
# l1:[],
return self.l2[-1]
def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
if self.l2 or self.l1:
return False
else:
return True
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
堆
Python字符串常考算法题
字符串
组合与继承
类变量和实列变量的区别
classmethod、staticmethod
什么是元类、使用场景
使用type定义元类,下面X的生成是等效的
ex 2
ex 3
new 与 init 的区别
继承元类ex
Python装饰器
如何使用类编写装饰器
如何给装饰器增加参数
设计模式
工厂模式
构造模式
Computer
ComputerBuilder
Engineer
原型模式
单例模式
单例模式
import threading
class Singleton(object):
_instance_lock = threading.Lock()
def __init__(self):
pass
def __new__(cls, *args, **kwargs):
if not hasattr(Singleton, "_instance"):
with Singleton._instance_lock:
if not hasattr(Singleton, "_instance"):
Singleton._instance = object.__new__(cls)
return Singleton._instance
ex 2
class Singleton2:
_obj = None
_init_flag = False
def __new__(cls, *args, **kwargs):
if not cls._obj:
cls._obj = object.__new__(cls)
return cls._obj
def __int__(self, name):
if not Singleton2._init_flag:
print('init')
Singleton2._init_flag = True
结构型模式
代理模式
Python函数式编程常考题
函数式编程
什么是闭包
闭包执行流程
被装饰的函数带参数多层闭包
ex1
ex2
常见Linux
操作系统线程和进程常考题
线程安全
进程间通讯
python多进程
操作系统内存管理
虚拟内存
内存抖动
Python垃圾回收机制
标记清除
分代回收
网络协议, 浏览器输入url过程
TCP三次握手过程
TCP四次挥手过程
HTTP协议
Cookies 和Sessions
常见网络编程题
使用socket发送http请求
IO多路复用常用题
五种IO模型
提升并发能力
select/poll/epoll区别
Python 如何实现IO多路复用
网络并发库
Mysql
ACID四个基本特性
四种事务隔离级别
解决高并发场景下的插入重复
乐观锁和悲观锁
InnoDB VS MyISAM
Mysql 索引
B-Tree
查找结构进化史
B树
B+树
什么时候创建索引
创建索引的注意事项
索引失效
聚集索引和非聚集索引
聚集索引与辅助索引
如何排查慢查询
Sql语句
Redis
缓存
操作时间对比
Redis和Memcached
Redis实现的跳跃表
Redis持久化
Redis事务
Redis实现分布式锁
缓存的模式
缓存穿透问题
缓存击穿问题
缓存雪崩问题
Python WSGI
web安全问题
前后端分离
RESTful
系统设计考点