日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

day19_生成器

發布時間:2023/12/13 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 day19_生成器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

20180730 初次上傳

20180731?更新,4、列表生成式,以及部分注釋

#!/usr/bin/env python # -*- coding:utf-8 -*-# ********************day19_生成器 ******************* # ********************day19_生成器 ******************* # ********************day19_生成器 *******************''' # # 1、yield# # 2、yield舉例-->生孩子# # 3、yiel舉例--> 賣包子# # 4、列表生成式----->不使用yield 舉例--> 賣包子 # # # 生成列表,然后加載到內存當中,在進行使用 # # # 缺點1:占空間大 # # # 缺點2:效率低# # # 4.1、yield-->for三元運算的過程 # # # for三元運算見 人口普查 實例# # 5、yield 舉例-->人口普查 # # # 對所有的人口進行總和等計算# # 6、、yield 對文件 # # # 取出來的是字符串# # 7、不同變量對yield所在函數多次取值 # # # 某一變量 對yield的所在的生成器取完所有的值,定義一個新的變量,可以對生成器再次取值 # ## # 8、不同變量,對生成器,同時取值互不影響# # 9、send 迭代器中的方法 # # # yield 3相當于return控制的是函數的返回值,這里返回3 # # # x = yiled的另一個特性是:接受send傳過來的值,賦值給x # # # next方法的過程: t.__next__()(第一次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存當前狀態--> # # # --> t.__next__()(第2次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存當前狀態--> # # # -->...-->迭代結束 # # # send()方法的過程:t.send(22)(第一次send)--> 將11賦值個yield左邊的等式(如 firt = yield, firt得到的值是22 ) --> # # # --> 執行下面的程序,直到下一次的yield之前-->保存運行位置的狀態 -->重復上述過程--> 。。。-->結束 # ## # 10、單線程單觸發 # # # 這種方式,執行效率低下,可與下面的“單線程多觸發對比”# # 11、迭代器實現單線程多觸發 # # # 功能說明:一邊做包子,一邊把做好的包子拿給別人吃 # # # 可以同時執行觸發多個程序,'''# print("分割線".center(80,'-')) # --------------------------------------分割線--------------------------------------- # --------------------------------------分割線--------------------------------------- # --------------------------------------分割線---------------------------------------# 01 # 01 # 01# # 1、yield # # def test(): # yield 1 # yield 2 # yield 3 # yield "沒有了,再來就報錯了" # res = test() # print("迭代器地址:",res) # print("第一個:",res.__next__()) # print("第2 個:",res.__next__()) # print("第3 個:",res.__next__()) # print("第4 個:",res.__next__()) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # 迭代器地址: <generator object test at 0x00000000039AFA40> # # 第一個: 1 # # 第2 個: 2 # # 第3 個: 3 # # 第4 個: 沒有了,再來就報錯了 # # # # Process finished with exit code 0# # 2、yield舉例-->生孩子 # # # import time # def test(): # print("開始生孩子啦...") # print("開始生孩子啦...") # print("開始生孩子啦...") # yield "me" # return # time.sleep(3) # # print("開始生'son'啦...") # yield "son" # time.sleep(3) # # print("開始生'Sunzi'啦...") # yield "SunZi" # time.sleep(3) # # yield "沒有了,再來就報錯了" # # res = test() # print("迭代器地址:",res) # print("第一個:",res.__next__()) # print("第2 個:",res.__next__()) # print("第3 個:",res.__next__()) # print("第4 個:",res.__next__()) # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # 迭代器地址: <generator object test at 0x000000000399FA40> # # 開始生孩子啦... # # 開始生孩子啦... # # 開始生孩子啦... # # 第一個: me # # 開始生'son'啦... # # 第2 個: son # # 開始生'Sunzi'啦... # # 第3 個: SunZi # # 第4 個: 沒有了,再來就報錯了 # # # # Process finished with exit code 0# # 3、yiel舉例--> 賣包子 # # # 使用yield 的好處就是,yield每次只迭代一個值,同時不再用內存,可以在執行某個功能回來后, # # # 從之前的位置,繼續向下取值 # # # # def product_bun(): # for i in range(100): # print("正在生產包子") # yield("一提包子 %s出來啦!"%i) # print("賣包子") # # pro_b = product_bun() # # bun1 = pro_b.__next__() # print("看-->",bun1) # # 代碼實現某個特定功能 # print("=======做某事,回來后再繼續吃包子=========") # bun2 = pro_b.__next__() # print("看-->",bun2) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # 正在生產包子 # # 看--> 一提包子 0出來啦! # # =======做某事后,回來再繼續吃包子========= # # 賣包子 # # 正在生產包子 # # 看--> 一提包子 1出來啦! # # # # Process finished with exit code 0# # https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431779637539089fd627094a43a8a7c77e6102e3a811000 # # 4、列表生成式----->不使用yield 舉例--> 賣包子 # # # 生成列表,然后加載到內存當中,在進行使用 # # # 缺點1:占空間大 # # # 缺點2:效率低 # # # # def product_bun(): # ret = [] # for i in range(5): # # print("正在生產包子") # ret.append("包子第 %s 盤出來啦!"%i) # # print("賣包子") # return ret # # bun_l = product_bun() # print(bun_l) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # ['包子第 0 盤出來啦!', '包子第 1 盤出來啦!', '包子第 2 盤出來啦!', '包子第 3 盤出來啦!', '包子第 4 盤出來啦!'] # # # # Process finished with exit code 0# 03 # 03 # 03 # # # 4.1、yield-->for三元運算的過程 # # # for三元運算見 人口普查 實例 # # # # # # # def xiadan(): # for i in range(3): # yield '雞蛋%s' %i # alex_lmj = xiadan() # print(alex_lmj.__next__()) # print(alex_lmj.__next__()) # print(alex_lmj.__next__()) # # print(alex_lmj.__next__()) # StopIteration# # 5、yield 舉例-->人口普查 # # # 對所有的人口進行總和等計算 # # # # # 文件:人口普查.txt ''' {'name':'北京','population':1} {'name':'山東','population':2} {'name':'山西','population':3} {'name':'河北','population':4} {'name':'臺灣','population':5} ''' # # def get_population(): # with open('人口普查','r',encoding='utf-8') as f: # for i in f: # yield i # g = get_population() # print(g) # # all_pop = sum( eval(i)['population'] for i in g) # print(all_pop) # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # <generator object get_population at 0x000000000399FAF0> # 15 # # Process finished with exit code 0# # 6、、yield 對文件 # # # 取出來的是字符串 # # # # # 文件:人口普查.txt ''' {'name':'北京','population':1} {'name':'山東','population':2} {'name':'山西','population':3} {'name':'河北','population':4} {'name':'臺灣','population':5} ''' # # def get_population(): # with open('人口普查','r',encoding='utf-8') as f: # for i in f: # yield i # g = get_population() # print(g) # g1 = g.__next__() # 注意: 取出來的值是字符串 # print( g1,type(g1)) # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # <generator object get_population at 0x000000000399FAF0> # # {'name':'北京','population':1} # # <class 'str'> # # # # Process finished with exit code 0 # # # # # ## # # 7、不同變量對yield所在函數多次取值 # # # 某一變量 對yield的所在的生成器取完所有的值,定義一個新的變量,可以對生成器再次取值 # # # # # 文件:人口普查.txt ''' {'name':'北京','population':1} {'name':'山東','population':2} {'name':'山西','population':3} {'name':'河北','population':4} {'name':'臺灣','population':5} ''' # def get_population(): # with open('人口普查','r',encoding='utf-8') as f: # for i in f: # yield i # g = get_population() # print(g) # all_pop = sum( eval(i)['population'] for i in g) # print("總人口數:",all_pop) # # print(g,g.__next__()) # StopIteration表示值已經取完了 # # m = get_population() # 可以對它再起取值 不影響 # print(m,m.__next__()) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # <generator object get_population at 0x000000000399FAF0> # # 總人口數: 15 # # <generator object get_population at 0x000000000399FCA8> {'name':'北京','population':1} # # # # # # Process finished with exit code 0# # 8、、不同變量,對生成器,同時取值互不影響 # # # # # 文件:人口普查.txt ''' {'name':'北京','population':1} {'name':'山東','population':2} {'name':'山西','population':3} {'name':'河北','population':4} {'name':'臺灣','population':5} ''' # def get_population(): # with open('人口普查','r',encoding='utf-8') as f: # for i in f: # yield i # g = get_population() # m = get_population() # 不同變量,對生成器,同時取值 # # print('g地址: ',g) # print('m地址: ',m) # print('g.__next__()',g.__next__()) # 不同變量,對生成器,同時取值互不影響 # print('m.__next__()',m.__next__()) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # g地址: <generator object get_population at 0x000000000399FAF0> # # m地址: <generator object get_population at 0x000000000399FCA8> # # g.__next__() {'name':'北京','population':1} # # # # m.__next__() {'name':'北京','population':1} # # # # # # Process finished with exit code 0 ## 05 # 05 # 05# # # 9、send 迭代器中的方法 # # # yield 3相當于return控制的是函數的返回值,這里返回3 # # # x = yiled的另一個特性是:接受send傳過來的值,賦值給x # # # next方法的過程: t.__next__()(第一次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存當前狀態--> # # # --> t.__next__()(第2次next) --> 拿到yield的返回值(如 yield 1 等同于 return1) -->保存當前狀態--> # # # -->...-->迭代結束 # # # send()方法的過程:t.send(22)(第一次send)--> 將11賦值個yield左邊的等式(如 firt = yield, firt得到的值是22 ) --> # # # --> 執行下面的程序,直到下一次的yield之前-->保存運行位置的狀態 -->重復上述過程--> 。。。-->結束 # # # # def test(): # print("開始啦……") # firt = yield 1 # 相當于,reutrn 1, firt =None, 運行狀態保留在這里 # print("第一次,yield之后",firt) # # yield 2 # print("第2次", ) # # t = test() # 這里并沒有運行 # res = t.__next__() # next()內置函數方法也可以 # print("第一次調用next:",res) # # res1 =t.send("函數停留在first那個位置,我就是給first賦值的") # 字符串發送給 yiled,使得,firt = yiled = 字符串 # print("第一次調用send:",res1) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # 開始啦…… # # 第一次調用next: 1 # # 第一次,yield之后 函數停留在first那個位置,我就是給first賦值的 # # 第一次調用send: 2 # # # # Process finished with exit code 0# # 10、單線程單觸發 # # # 這種方式,執行效率低下,可與下面的“單線程多觸發對比” # import time # def produce(): # ret = [] # for i in range(3): # time.sleep(1) # ret.append('包子%s'%i) # return ret # # def consumer(res): # for index, baozi in enumerate(res): # time.sleep(1) # print('第%s個人,吃了%s'%(index,baozi)) # # print("開始點包子啦……") # res = produce() # 列表生成后才回繼續下面的程序 # print("開吃咯……") # consumer(res) # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # 開始點包子啦…… # # 開吃咯…… # # 第0個人,吃了包子0 # # 第1個人,吃了包子1 # # 第2個人,吃了包子2 # # # # Process finished with exit code 0# # 11、迭代器實現單線程多觸發 # # # 功能說明:一邊做包子,一邊把做好的包子拿給別人吃 # # # 可以同時執行觸發多個程序, # # # # import time # # def consumer(name): # print("我是【%s】,我準備開始吃包子啦", name) # while True: # bun = yield # time.sleep(1) # print("%s 很開心的把【%s】吃掉啦"%(name,bun) ) # # def producer(): # c1 = consumer("--wu--") # c2 = consumer(" sb ") # c1.__next__() # 開始運行迭代器 # c2.__next__() # for i in range(3): # time.sleep(1) # c1.send("包子 %s" %i) # c2.send("包子 %s" %i) # # producer() # # # # D:\Anaconda3\python.exe D:/C_cache/py/day19_ShengChengQi/day19_ShengChengQi.py # # 我是【%s】,我準備開始吃包子啦 --wu-- # # 我是【%s】,我準備開始吃包子啦 sb # # --wu-- 很開心的把【包子 0】吃掉啦 # # sb 很開心的把【包子 0】吃掉啦 # # --wu-- 很開心的把【包子 1】吃掉啦 # # sb 很開心的把【包子 1】吃掉啦 # # --wu-- 很開心的把【包子 2】吃掉啦 # # sb 很開心的把【包子 2】吃掉啦 # # # # Process finished with exit code 0

?

轉載于:https://www.cnblogs.com/jyfootprint/p/9409915.html

總結

以上是生活随笔為你收集整理的day19_生成器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。