本文主要是介绍python基础-生成器进阶(send、from)、各种推导式、生成器表达式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
-
-
- yield回顾
- send
- 生成器结合装饰器
- yield from
- 列表推导式
- 字典推导式
- 集合推导式
- 生成器表达式
-
-
yield回顾
def cloth():for i in range(10):yield '衣服%s'%ig = cloth()
for i in g:print(i)g = cloth()
for i in range(10):print(g.__next__())g = cloth()
lst = list(g)
for i in lst:print(i)def method():yield 2,3,4y = method()
for i in y.__next__():print(i)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/2生成器函数.py
衣服0
衣服1
衣服2
衣服3
衣服4
衣服0
衣服1
衣服2
衣服3
衣服4
衣服0
衣服1
衣服2
衣服3
衣服42
3
4
send
def func():print('*'*10)a = yield 5print('a : ',a)yield 10
g = func()
num = g.__next__()
print(num)
num = g.__next__()
print(num)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/2生成器函数.py
**********
5
a : None
10
我们看结果a:None是没有输出内容,是因为我们需要引入一个方法send
我们修改下代码如下:
def func():print('*'*10)a = yield 5print('a : ',a)yield 10
g = func()
num = g.__next__()
print(num)num2 = g.send('alex')
print(num2)
输出如下
E:\python\python_sdk\python.exe E:/python/py_pro/2生成器函数.py
**********
5
a : alex
10
从哪一个yield开始接着执行,就把一个值传给了那个yield
send不能用在第一个触发生成器
生成器函数中有多少个yield就必须有多少个next+send
生成器结合装饰器
生成器的预激装饰器
def init(func): #生成器的预激装饰器def inner(*args,**kwargs):g = func(*args,**kwargs) #func = averagerprint(g.__next__())return greturn inner@init
def averager():term = yield 0print(term)yield 1g_avg = averager()
print(g_avg.send(19))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/2生成器函数.py
0
19
1
yield from
我们来看下面的一个错误的例子
def func():a = 1yield from ag = func()
print(g.__next__())
错误日志如下:
File "E:/python/py_pro/2生成器函数.py", line 18, in <module>print(g.__next__())File "E:/python/py_pro/2生成器函数.py", line 14, in funcyield from a
TypeError: 'int' object is not iterable
说明是必须可以迭代的
def func():a = "safly"yield from ag = func()
for i in g:print(i)
输出如下:
s
a
f
l
y
当然其他的元组、列表也是可以的
print("-------列表-----")
def func():a = [1,2,3]yield from ag = func()
print(g.__next__())
print(g.__next__())
print(g.__next__())
print("-------元组-----")
def func():a = (1,2,3)yield from a
g = func()
for i in g:print(i)print("-------字典-----")
def func():a = {"a":1,"b":2}yield from ag = func()
for i in g:print(i)
print("-------组合-----")
def func():a = 'AB'b = [1,2]yield from ayield from by = func()
print(y.__next__())
print(y.__next__())
print(y.__next__())
print(y.__next__())
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/2生成器函数.py
-------列表-----
1
2
3
-------元组-----
1
2
3
-------字典-----
a
b
-------组合-----
A
B
1
2Process finished with exit code 0
列表推导式
y = [1,2,3,4,5,6,7,8]
x = [i*i for i in y]
print(x)x2 = [i/2 for i in range(10)]
print(x2)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3生成器表达式.py
[1, 4, 9, 16, 25, 36, 49, 64]
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
继续看列表推导式带if条件的
print(list(range(0,30,3)))
print([i for i in range(30) if i%3 == 0])
输出如下:
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
字典推导式
mcase = {'a': 10, 'b': 34}
mcase_frequency = {mcase[k]:k for k in mcase}
print(mcase_frequency)mcase = {'a': 10, 'b': 34, 'A': 7, 'Z': 3}
mcase_frequency = {k.lower(): (mcase.get(k.lower(), 0) + mcase.get(k.upper(), 0)) for k in mcase}
print(mcase_frequency)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/4各种推导式扩展.py
{10: 'a', 34: 'b'}
{'a': 17, 'b': 34, 'z': 3}
集合推导式
squared = {x**2 for x in [1, -1, 2]} #{1,4}
print(squared)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/4各种推导式扩展.py
{1, 4}
生成器表达式
y = [1,2,4]
g = (i*i for i in y)
print(g)
print(list(g))y = [1,2,4]
g = (i*i for i in y)
for i in g:print(i)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3生成器表达式.py
<generator object <genexpr> at 0x03116240>
[1, 4, 16]
1
4
16
这篇关于python基础-生成器进阶(send、from)、各种推导式、生成器表达式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!