day12-函数生成器表达式、三元表达式、列表推导式、

一 什么是生成器

只要函数内部包含有yield关键字,那么函数名()的到的结果就是生成器,可以通过next()方法逐个调用值,直到结束会报错并且不会执行函数内部代码,
该值称之为生成器
 1 def func():
 2     print('====>first')
 3     yield 1
 4     print('====>second')
 5     yield 2
 6     print('====>third')
 7     yield 3
 8     print('====>end')
 9 
10 g=func()
11 print(g) #<generator object func at 0x0000000002184360> 
12 next(g)
13 print(g) #执行 print('====>first')并打印出yield的返回值为 :1
14 next(g)
15 next(g)
16 next(g)     #该步会报错,因为没有yield抛出异常 StopIteration
View Code

二 生成器就是迭代器

生成器的本质就是迭代器

先通过调用内置__iter__ 方法将迭代对象编程迭代器对象,在通过__next__方法取值

g.__iter__
g.__next__
所以生成器就是迭代器,因此生成器可以这么取值 跟 迭代器一样
res=next(g)
print(res)

 三 yield 总结

总结yield的功能
1、提供一种自定义迭代器的方式
2、yield可以暂停住函数,可以传返回值,通过print()打印出返回值

yield VS return
相同点:都是用在函数内,都可以返回值,没有类型限制,没有个数限制,如果是返回多个值,那返回值会被存储在元祖里
不同点:return只能返回一次值,yield可以返回多次值

yield可以避免程序执行时候相同的变量值占用内存,造成机器卡死,执行一行后结束,等待下一个next()来继续调用取值

事例 :通过yield自定义实现range函数

def my_range(start,stop,step=1):
     while start < stop:
         yield start # 暂停
         start+=step
g=my_range(1,5,2) #1 3

print(next(g))
print(next(g))
print(next(g))
print(next(g))

关于yield补充

yield 接收回值、通过send()向yield传值 

 1 def eat(name):
 2     food_list=[]
 3     print('somebody %s 准备开吃' %name)
 4     while True:
 5         food=yield food_list#暂停 food=yield='一桶泔水'
 6         print('somebody [%s]吃了<%s>' %(name,food))
 7         food_list.append(food)
 8 
 9 hellen=eat('isetan')
10 
11 res1=next(hellen) # 初始化
12 print(res1)
13 
14 res2=hellen.send(('蛋糕','肯德基')) #第一步用send传值,然后通过next()取值
15 print(res2)
16 
17 res3=hellen.send('必胜客')
18 print(res3)
19 
20 打印结果为:
21 somebody isetan 准备开吃
22 []
23 somebody [isetan]吃了<('蛋糕', '肯德基')>
24 [('蛋糕', '肯德基')]
25 somebody [isetan]吃了<必胜客>
26 [('蛋糕', '肯德基'), '必胜客']
View Code

四 、三元表达式和列表推导式 

(1)三元表达式--即条件成立结果为wcl 如果条件不成立结果为isetan

name=input('姓名>>: ')
res='wcl' if name == 'wcl' else 'isetan'
print(res)

(2)列表生成式

格式:lst = [experssion for condition if condition ]

(1)for循环 生成10个鸡蛋
egg_list=[]
for i in range(10):
    egg_list.append('鸡蛋%s' %i)
(2)列表生成式 生成10个鸡蛋
egg_list=['鸡蛋%s' %i for i in range(10)]

列表生成式事例

 1 (1 2 l=[item**2 for item in range(1,11)]
 3 print(l)
 4 (2 5 names=['isetan','wxx','lxx']
 6 l=[]
 7 #for 循环
 8 for name in names:
 9      l.append(name + 'haha')
10 names=l
11 print(names)
12 #列表生成式
13 names=[name+'haha' for name in names]
14 print(names)
15 (3)for +条件 if 条件
16 names=['wcl','wxx','isetan','lxx','zhangmingyan']
17 names=[name+'haha' for name in names if name != 'isetan']
18 print(names)
View Code

(3)字典生成式、集合生成式

格式:lst = {experssion for condition if condition}

 1 (1)字典生成式
 2 keys=['name','age','sex']
 3 values=['wcl',18,'male']
 4 
 5 res=zip(keys,values) #拉链函数
 6 
 7 d={k:v for k,v in zip(keys,values)}
 8 print(d)
 9 
10 (2)字典生成式
11 values=['wcl',18,'male']
12 d = {k,for k in values }
View Code

(4)生成器表达式

格式:lst = (experssion for condition if condition)

ps:列表生成式是用[] 生成器表达式是用()

 1 在调用next时候将in 后面的可迭代对象变成迭代对象,通过next()调取值
 2 g=(i for i in range(10))
 3 print(g)  ###<generator object <genexpr> at 0x0000000002792D58>
 4 print(next(g))
 5 print(next(g))
 6 
 7 打印结果为:
 8 <generator object <genexpr> at 0x0000000002792D58>
 9 0
10 1
View Code

  

  

 

猜你喜欢

转载自www.cnblogs.com/wcl0517/p/9178105.html