num=[1,2,3]
#列表推导式
myvec=[[x,x*2] for x in num] #列表推导式:[[1, 2], [2, 4], [3, 6]]
# 返回生成器节省内存
myvec=([x,x*2] for x in num) #<generator object <genexpr> at 0x0000020E7EE4D970>
# zip拉链操作
# 同时遍历三个数组
x = [[1], 2, 3, 4, 5]
y = ['a', 'b', 'c', 'd', 'e']
z = ['a1', 'b2', 'c3', 'd4', 'e5']
for i in zip(x,y,z):
print(i)
# 不等长序列遍历
from itertools import zip_longest#
x = [1, 2, 3, 4, 5, 6]
y = ['a', 'b', 'c', 'd', 'e']
for i in zip_longest(x,y):
print(i)
# 避免过多循环
for x in ['a','b','c']:
for y in ['d','e','f']:
for z in ['m','n']:
print(x,y,z)
from itertools import product
for x,y,z in product(['a','b','c'],['d','e','f'],['m','n']):
print(x,y,z)
# 偏函数
# functools.partial冻结参数
from operator import mul,add,abs
from functools import partial
triple = partial(mul, 3)
print(triple(7))
triple1 = partial(add, 10)
print(triple1(2))
triple2 = partial(abs)
print(triple2(-2))
#函数缓存
# LRU(Least Recently Used)把耗时的函数结果保存起来,避免传入相同的
# 参数重复计算。的缓存不会无限储存,一段时间不用,或者数量超出一定限制,
# 旧缓存就会扔掉。
# from functools import lru_cache#参数maxsize为最多缓存的次数,如果
# 为None,则无限制,设置为2n时,性能最佳;typed如果设置为True,表
# 示不同参数类型结构分开保存,如(1和1.0)区分开
import time
from functools import lru_cache
@lru_cache(maxsize=None,typed=False)
def add(x, y):
print("calculating: %s + %s" % (x, y))
time.sleep(5)
return x + y
print(add(1, 2)) #耗时5秒
print(add(1, 2)) #耗时0.秒
print(add(2, 3)) #耗时5秒
class MyIter(object):
def __init__(self, total, start=0):
self.total = total
self.start = start
def __iter__(self):
return self
def __next__(self):
if self.start < self.total:
self.start += 1
return self.start
else:
raise StopIteration
myIter = MyIter(5)
print(myIter)
it = iter(myIter)
for d in it:
print (d)
# 反射机制,反射就是通过字符串的形式,导入模块;通过字符串的形式,去
# 模块寻找指定函数,并执行。利用字符串的形式去对象(模块)中操作(查
# 找/获取/删除/添加)成员,一种基于字符串的事件驱动
# getattr()
# hasattr()
# setattr()
# delattr()
class aa():
def gg(self):
print(22)
a=aa()
p=getattr(a,"gg")
p() #22
# 魔术方法就是一个类/对象中的方法,和普通方法唯一的不同时,普通方法需
# 要调用!而魔术方法是在特定时刻自动触发。
# 用于比较的魔术方法:
# __cmp__(self, other) 是比较方法里面最基本的的魔法方法
# __eq__(self, other) 定义相等符号的行为,==
# __ne__(self,other) 定义不等符号的行为,!=
# __lt__(self,other) 定义小于符号的行为,<
# __gt__(self,other) 定义大于符号的行为,>
# __le__(self,other) 定义小于等于符号的行为,<=
# __ge__(self,other) 定义大于等于符号的行为,>=
class Super(object):
def __init__(self,ane):
self.value=ane
print("*")
def __new__(cls, *args, **kwargs):
print("**")
return super(Super,cls).__new__(cls)
def __eq__(self, other):
print(other)
def __len__(self):
return len(self.value)
def __pos__(self):
print(32)
def __add__(self, other):
return self.value+other
def __int__(self):
return int(self.value)
Super(2)
print(Super(6)+10)
Super(12)==23
print(int(Super("99")))
@staticmethod、@classmethod区别
调用方式一致的,不需实例化,#class.def()
@classmethod:def test(cls):只能用cls.调用类属性和类方法
@staticmethodtest():不能调用类属性、实例属性和类方法,什么都不能调用
@property 和他们有区别,
调用方式:需要实例化,但是该方法后面不需要加(),#class().def
能调用所有属性和方法(包括类方法和静态方法)
#生成器,函数结论yeild变为生成器
def asd():
for i in range(10):
a = {}
a[i]=i*i
yield a
a=asd()
print(list(a)) #[{0: 0}, {1: 1}, {2: 4}, {3: 9}, {4: 16}, {5: 25}, {6: 36}, {7: 49}, {8: 64}, {9: 81}]
def fib(max):
a, b = 1, 1
while a < max:
yield a
a, b = b, a + b
for n in fib(15):
print(n)
# 1
# 1
# 2
# 3
# 5
# 8
# 13
m = fib(13)
print(m) #<generator object fib at 0x000001CBD8787B30>
print(m.__next__()) #1
print(m.__next__()) #1
print(m.__next__()) #2
print(m.__next__()) #3
print(list(m)) #[5, 8]
未完待续,持续更新中…