python 列表表达式、生成器表达式和协程函数
列表表达式、生成器表达式和协程函数
一、列表表达式:
常规方式示例:
egg_list=[]
for i in range(100):
egg_list.append("egg%s"%i)
print(egg_list)
执行结果是:
['egg0', 'egg1', 'egg2', 'egg3', 'egg4', 'egg5', 'egg6', 'egg7', 'egg8', 'egg9', 'egg10', 'egg11', 'egg12', 'egg13', 'egg14', 'egg15', 'egg16', 'egg17', 'egg18', 'egg19', 'egg20', 'egg21', 'egg22', 'egg23', 'egg24', 'egg25', 'egg26', 'egg27', 'egg28', 'egg29', 'egg30', 'egg31', 'egg32', 'egg33', 'egg34', 'egg35', 'egg36', 'egg37', 'egg38', 'egg39', 'egg40', 'egg41', 'egg42', 'egg43', 'egg44', 'egg45', 'egg46', 'egg47', 'egg48', 'egg49', 'egg50', 'egg51', 'egg52', 'egg53', 'egg54', 'egg55', 'egg56', 'egg57', 'egg58', 'egg59', 'egg60', 'egg61', 'egg62', 'egg63', 'egg64', 'egg65', 'egg66', 'egg67', 'egg68', 'egg69', 'egg70', 'egg71', 'egg72', 'egg73', 'egg74', 'egg75', 'egg76', 'egg77', 'egg78', 'egg79', 'egg80', 'egg81', 'egg82', 'egg83', 'egg84', 'egg85', 'egg86', 'egg87', 'egg88', 'egg89', 'egg90', 'egg91', 'egg92', 'egg93', 'egg94', 'egg95', 'egg96', 'egg97', 'egg98', 'egg99']
使用列表表达式的方式是:(2行代码即可)
示例:
l=["egg%s"%i for i in range (100)]
print(l)
执行结果是:
['egg0', 'egg1', 'egg2', 'egg3', 'egg4', 'egg5', 'egg6', 'egg7', 'egg8', 'egg9', 'egg10', 'egg11', 'egg12', 'egg13', 'egg14', 'egg15', 'egg16', 'egg17', 'egg18', 'egg19', 'egg20', 'egg21', 'egg22', 'egg23', 'egg24', 'egg25', 'egg26', 'egg27', 'egg28', 'egg29', 'egg30', 'egg31', 'egg32', 'egg33', 'egg34', 'egg35', 'egg36', 'egg37', 'egg38', 'egg39', 'egg40', 'egg41', 'egg42', 'egg43', 'egg44', 'egg45', 'egg46', 'egg47', 'egg48', 'egg49', 'egg50', 'egg51', 'egg52', 'egg53', 'egg54', 'egg55', 'egg56', 'egg57', 'egg58', 'egg59', 'egg60', 'egg61', 'egg62', 'egg63', 'egg64', 'egg65', 'egg66', 'egg67', 'egg68', 'egg69', 'egg70', 'egg71', 'egg72', 'egg73', 'egg74', 'egg75', 'egg76', 'egg77', 'egg78', 'egg79', 'egg80', 'egg81', 'egg82', 'egg83', 'egg84', 'egg85', 'egg86', 'egg87', 'egg88', 'egg89', 'egg90', 'egg91', 'egg92', 'egg93', 'egg94', 'egg95', 'egg96', 'egg97', 'egg98', 'egg99']
如果想加入if判断也非常便利
示例:
l=["egg%s"%i for i in range (100) if i >90]
print(l)
l=["egg%s"%i for i in range (100) if i >90 if i>95]
print(l)
执行结果是:
['egg91', 'egg92', 'egg93', 'egg94', 'egg95', 'egg96', 'egg97', 'egg98', 'egg99']
['egg96', 'egg97', 'egg98', 'egg99']
如果需要使用多个for循环的话
常规方式:
示例:
import os#导入模块
g=os.walk("C:\\SWTOOLS")#查看电脑C盘下的SWTOOLS
l=[]
for i in g :
for j in i :
l.append("%s\\%s"%(i[0],j))
print(l)
执行结果是:
['C:\\SWTOOLS\\C:\\SWTOOLS', "C:\\SWTOOLS\\['Theme']", 'C:\\SWTOOLS\\[]', 'C:\\SWTOOLS\\Theme\\C:\\SWTOOLS\\Theme', 'C:\\SWTOOLS\\Theme\\[]', 'C:\\SWTOOLS\\Theme\\[]']
使用列表表达式的方式是:
示例:
import os#导入模块
g=os.walk("C:\\SWTOOLS")#查看电脑C盘下的SWTOOLS
l=["%s\\%s"%(i[0],j)for i in g for j in i ]
print(l)
执行结果是:
['C:\\SWTOOLS\\C:\\SWTOOLS', "C:\\SWTOOLS\\['Theme']", 'C:\\SWTOOLS\\[]', 'C:\\SWTOOLS\\Theme\\C:\\SWTOOLS\\Theme', 'C:\\SWTOOLS\\Theme\\[]', 'C:\\SWTOOLS\\Theme\\[]']
二、生成器表达式:
示例:
l=("egg%s"%i for i in range (100))
print(l)
print(l.__next__())#每一次._next_一下,取出一个值
print(l.__next__())
print(l.__next__())
print(l.__next__())
print(l.__next__())
print(l.__next__())
print(l.__next__())##超出边界,当for循环结束时,捕捉到StopIteration异常,终止迭代
执行结果是:
<generator object <genexpr> at 0x0000000001DDFF68>
egg0
egg1
egg2
egg3
egg4
egg5
egg6
总结:
1.把列表解析的[]换成()得到的就是生成器表达式
2.列表解析与生成器表达式都是一种便利的编程方式,只不过生成器表达式更节省内存
三、协程函数
即生成器yield的表达式的形式
示例:
def eater (name):
print("%s start to eat"%name)
food_list=[]
while True:
food=yield food_list
print("%s eat %s"%(name,food))
food_list.append(food)
e=eater("tom")#执行的是foo_1(“tom”)
next(e)#触发函数运行
print(e.send("鸡蛋"))
print(e.send("苹果"))
执行结果是:
tom start to eat
tom eat 鸡蛋
['鸡蛋']
tom eat 苹果
['鸡蛋', '苹果']