day12
Builder
Iterator: python built a space-saving tool
The nature of the generator is a iterator
The difference between iterators and generators: one pyhton comes with a programmer to write their own
Write a generator
Based Functions
In the function return is rewritten to yield a generator
Function name () is to produce a generator
return could write more, but only the implementation of a
yield can write more, you can also return many times
The __next a __ ** () corresponds to the yield ** a
______ next ______ () == next (): recommended next ()
______ ______ trip () == trip ()
yield is returned, it will record the position of the Executive
Generator may be used for obtaining loop
from the yield - the elements one by one return iterables
Temporary pause in yield can function inside a for loop and while loops
Benefits: save space - inert mechanism
Not retrograde
Disposable
Corresponding to a yield a next
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('香肠')
Builder application scenarios
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)
Derivations
List comprehensions:
Circulating derivation: [variables (variables after processing) for circulating]
Filter derivation: [variables (variables after processing) for circulating the processing conditions]
# 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)
Builder derivations:
Circulating derivations :( variables (variables after processing) for loop)
Screening derivations :( variables (variables after processing) for circulating the processing conditions)
# 循环模式:
# 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 derivations: set ()
{Key: values of processing conditions for loop}
# 集合推导式:(了解)
# 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)}))
Dictionary derivation: {}
{Variables (variables after processing) processing conditions for loop}
# 字典推导式:(了解)
# 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)
A built-in function
eval (): calculated string inside the answer
exec (): String inside the code the answer
Work and study can not be used
hash (): Analyzing the data type is not immutable
help (): get the source code
callable (): judgment is not callable
int()
float (): is converted to floating point
complex (): plural
bin (): converted to binary
oct (): converted to octal
hex (): converted to hexadecimal
divmod (5,2) :( 2,1) quotient more than 2 1
round (): five off and six, odd change even change, the default is an integer, you can specify several reservations
pow (2,3): power
bytes (s, encoding = 'utf-8'): conversion of byte
ord (): ascii code bits to code the current
chr (): find the code bit with the contents
repr (): prototype absolutely shattered
all (): determine whether the elements are true
any (): determining whether there is true element
globals (): check the global variable space
locals (): View the current space variables, there is printed dictionary, not just print empty dictionary
# 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()) # 查看当前空间的变量