day12
生成器
迭代器:python中内置的一种节省空间的工具
生成器的本质就是一个迭代器
迭代器和生成器的区别:一个是pyhton自带的,一个是程序员自己写的
写一个生成器
基于函数
在函数中将return改写成yield就是一个生成器
函数名()就是产生一个生成器
return 可以写多个,但是只执行一个
yield也可以写多个,还可以返回多次
一个__next__()对应一个yield
next() == next():推荐使用next()
iter() == iter()
yield 也是返回,会记录执行位置
生成器可以使用for循环获取
yield from——将可迭代对象元素逐一返回
在函数的内部 yield 能将for循环和while循环进行临时暂停
优点:节省空间——惰性机制
不能逆行
一次性
一个next对应一个yield
def func():
print(123)
return '你好'
func()
---------------------------------
def func():
if 3>2:
yield '你好'
if 4>2:
yield '我好'
yield '大家好'
g = func() # 产生一个生成器
print(next(g))
print(next(g))
print(next(g))
for i in g:
print(1)
while True:
try:
print(g.__next__())
except StopIteration:
break
---------------------------------
def foo():
for i in range(10):
pass
yield i
count = 1
while True:
yield count
count += 1
g = foo()
# print(next(g)) # 推荐使用
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# for i in g:
# print(i)
坑 —— 会产生新的生成器
print(next(foo()))
print(next(foo()))
send()
# def gen(name):
# print(f'{name} ready to eat')
# while 1:
# food = yield
# print(f'{name} start to eat {food}')
------------------------------------------------------
# dog = gen('alex')
# next(dog)
# next(dog)
# next(dog)
------------------------------------------------------
# def gen(name):
# # print(f'{name} ready to eat')
# while 1:
# food = yield 222
# print(f'{name} start to eat {food}')
# dog = gen('alex')
# next(dog) # 第一次必须用next让指针停留在第一个yield后面
# 与next一样,可以获取到yield的值
# ret = dog.send('骨头')
# print(ret)
------------------------------------------------------
# def gen(name):
# print(f'{name} ready to eat')
# while 1:
# food = yield
# print(f'{name} start to eat {food}')
------------------------------------------------------
# dog = gen('alex')
# next(dog)
# # 还可以给上一个yield发送值
# # next(dog)
# dog.send('骨头')
# dog.send('狗粮')
# dog.send('香肠')
生成器应用场景
def func():
lst = []
for i in range(10000):
lst.append(i)
return lst
print(func())
---------------------------------
def func():
for i in range(10000):
yield i
g = func()
for i in range(50):
print(next(g))
---------------------------------
def func():
lst = ["牛羊配","老奶奶花生米","卫龙","虾扯蛋","米老头","老干妈"]
for i in lst:
yield i
g = func()
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
---------------------------------
def func():
lst = ["牛羊配","老奶奶花生米","卫龙","虾扯蛋","米老头","老干妈"]
lst2 = ["小浣熊","老干爹","亲嘴烧","麻辣烫","黄焖鸡","井盖"]
yield from lst1
yield from lst2
g = func()
print(next(g))
print(next(g))
print(next(g))
# for i in g:
# print(i)
推导式
列表推导式:
循环推导式:[变量(加工后的变量) for循环]
筛选推导式:[变量(加工后的变量) for循环 加工条件]
# lst = []
# for i in range(20):
# lst.append(i)
# print(lst)
# list推导式
# print([i for i in range(20)])
------------------------------------------------------
# 循环模式
# [变量 for i in range(20)]
# print([i+1 for i in range(10)])
------------------------------------------------------
# 筛选模式
# lst = []
# for i in range(20):
# if i % 2 == 0:
# lst.append(i)
# print(lst)
# print([i for i in range(20) if i % 2 == 0])
# [变量(加工后的变量) for循环 加工方式]
# print([i for i in range(50) if i % 2 == 1])
# print([i for i in range(1,50,2)])
------------------------------------------------------
list二层嵌套for:
# print([i for i in range(20) for n in range(10)])
lst = []
for i in range(20):
for n in range(10):
lst.append(i)
print(lst)
生成器推导式:
循环推导式:(变量(加工后的变量) for循环)
筛选推导式:(变量(加工后的变量) for循环 加工条件)
# 循环模式:
# g = (i for i in range(20))
# print(next(g))
# print(next(g))
# print(next(g))
# print(list((i for i in range(20))))
------------------------------------------------------
# 筛选模式
# g = (i for i in range(50) if i % 2 == 1)
# for i in g:
# print(i)
------------------------------------------------------
二层for嵌套:
g = (i for i in range(20) for n in range(10))
for i in g:
print(i)
集合推导式:set()
{键:值 for循环 加工条件}
# 集合推导式:(了解)
# print({i for i in range(10)})
# print({i for i in range(10) if i % 2 == 0})
------------------------------------------------------
二层for嵌套:(去重)
print({i*n for i in range(20) for n in range(10)})
print(len({i*n for i in range(20) for n in range(10)}))
字典推导式:{}
{变量(加工后的变量) for循环 加工条件}
# 字典推导式:(了解)
# print({i:i+1 for i in range(10)})
# print({i:i+1 for i in range(10) if i % 2 == 0})
# {键:值 for循环 加工条件}
------------------------------------------------------
二层for嵌套:
print({i:n for i in range(20) for n in range(10,20)})
dic = {}
for i in range(20):
for n in range(20):
dic[i] = n
print(dic)
内置函数一
eval():字符串里面的计算公式得出答案
exec() :字符串里面的代码得出答案
工作和学习不能用
hash() :判断是不是不可变数据类型
help() :得到源码
callable() :判断是不是可调用
int()
float():转化为浮点数
complex():复数
bin() :转化为二进制
oct():转化为八进制
hex():转化为十六进制
divmod(5,2):(2,1)商2余1
round():五舍六入,奇变偶不变,默认是整数,可以指定保留几位
pow(2,3):幂
bytes(s,encoding='utf-8'):转化为字节
ord():当前编码 求ascii码位
chr():用码位求内容
repr():原型毕露
all():判断元素是否都为真
any():判断元素是否有真
globals():查看全局空间中的变量
locals():查看当前空间中的变量,有就打印字典,没有就打印空字典
# s = """
# for i in range(10):
# print(i)
# """
------------------------------------------------------
# s1 = """
# def func():
# print(123)
# func()
# """
# print(eval(s))
# print(exec(s1)) # 牛逼 不能用
------------------------------------------------------
# print(hash("asdfas"))
------------------------------------------------------
# print(help(list))
# help(dict)
------------------------------------------------------
# def func():
# pass
# print(callable(func)) # 查看是否可调用
------------------------------------------------------
# print(float(2)) # 浮点数
# print(complex(56)) # 复数
------------------------------------------------------
# print(oct(15)) # 八进制
# print(hex(15)) # 十六进制
------------------------------------------------------
# print(divmod(5,2)) # (2, 1) 2商 1余
------------------------------------------------------
# print(round(5.3234,2)) # 四舍五入 -- 默认是整数,可以指定保留小数位
------------------------------------------------------
# print(pow(2,3)) # 幂
# print(pow(2,3,4)) # 幂,余
------------------------------------------------------
# s = "alex"
# print(bytes(s,encoding="utf-8"))
------------------------------------------------------
# print(ord("你")) # 当前编码
# print(chr(20320))
------------------------------------------------------
# s = "C:\u3000"
# print(repr(s))
------------------------------------------------------
# print("\u3000你好")
------------------------------------------------------
# lst = [1,2,3,False,4,5,6,7]
# print(all(lst)) # 判断元素是否都为真 相似and
# print(any(lst)) # 判断元素是否有真 相似or
------------------------------------------------------
# name = 1
# def func():
# a = 123
# # print(locals())
# # print(globals())
# func()
------------------------------------------------------
# print(globals()) # 全局空间中的变量
# print(locals()) # 查看当前空间的变量