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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249842.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249842.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249842.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HTML渲染過程詳解

由于本人對http協議以及dns對url的解析問題并不了解&#xff0c;所以這里之探討url請求加載到瀏覽器端時&#xff0c;瀏覽器對html的解析到呈現過程&#xff0c;后來經過幾位道友分享&#xff0c;整理了一下url解析的過程&#xff0c;如下&#xff1a; 用戶輸入url地址&#x…

grid布局

CSS Grid 布局CSS 中最強大的布局系統。與 flexbox 的一維布局系統不同&#xff0c;CSS Grid 布局是一個二維布局系統&#xff0c;也就意味著它可以同時處理列和行。通過將 CSS 規則應用于 父元素 (成為 Grid Container 網格容器)和其 子元素&#xff08;成為 Grid Items 網格項…

vue之router-view組件的使用

開發的時候有時候會遇到一種情況&#xff0c;比如 &#xff1a;點擊這個鏈接跳轉到其他組件的情況&#xff0c;通常會跳轉到新的頁面&#xff0c;蛋是&#xff0c;我們不想跳轉到新頁面&#xff0c;只在當前頁面切換著顯示&#xff0c;那么就要涉及到路由的嵌套了&#xff0c;也…

go 學習Printf

package main import "fmt" import "os" type point struct {x, y int } func main() { //Go 為常規 Go 值的格式化設計提供了多種打印方式。例如&#xff0c;這里打印了 point 結構體的一個實例。p : point{1, 2}fmt.Printf("%v\n", p) // {1 2…

博客園使用latex編輯公式

如何開啟數學公式編輯功能 開啟方法見下鏈接https://www.cnblogs.com/cmt/p/3279312.html 功能 支持數學公式塊支持文中數學公式DEMO $$ f(n) \begin{cases}\frac{n}{2}, & \text{if $n$ is even} \\3n1, & \text{if $n$ is odd}\end{cases} $$ 以上的代碼產生如下的公…

console.dir有很多瀏覽器,系統的兼容性問題,不要隨便用!

console.dir有很多瀏覽器&#xff0c;系統的兼容性問題&#xff0c;不要隨便用&#xff01; 要使用console.log();轉載于:https://www.cnblogs.com/bluestear/p/9400356.html

go 區分指針

先看一段代碼 先放一段代碼&#xff0c;人工運行一下&#xff0c;看看自己能做對幾題&#xff1f; package mainimport "fmt"func main() {var a int 1 var b *int &a var c **int &b var x int *b fmt.Println("a ",a) fmt.Println("&a…

ajax和axios、fetch的區別

1.jQuery ajax $.ajax({type: POST,url: url,data: data,dataType: dataType,success: function () {},error: function () {} });傳統 Ajax 指的是 XMLHttpRequest&#xff08;XHR&#xff09;&#xff0c; 最早出現的發送后端請求技術&#xff0c;隸屬于原始js中&#xff0c…

函數函數sigaction、signal

函數函數sigaction 1. 函數sigaction原型&#xff1a; int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); 分析&#xff1a; 參數 signum &#xff1a;要捕獲的信號。參數act&#xff1a;truct sigaction 結構體&#xff0c;后面具體講解傳入…

使用SQL Server連接xml接口,讀取并解析數據

--數據源格式&#xff0c;放到任意程序中部署接口即可--<Data xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd"http://www.w3.org/2001/XMLSchema"> --<Peoples> --<People> --<Name>張三</Name> --<S…

mac 卸載編輯器卸不干凈

Configuration~/Library/Preferences/Caches~/Library/Caches/Plugins~/Library/Application Support/Logs~/Library/Logs/轉載于:https://www.cnblogs.com/smzd/p/10114540.html

vue中使用axios最詳細教程

前提條件&#xff1a;vue-cli 項目 安裝&#xff1a; npm npm 在main.js導入&#xff1a; // 引入axios&#xff0c;并加到原型鏈中 import axios from axios; Vue.prototype.$axios axios; import QS from qs Vue.prototype.qs QS;封裝好的axios,拿走不送&#xff1a;&am…

Java 類型轉換String,List,Map,Array

1. JsonString轉為Map String jsoNStr "{\n" "\t\"TestArray\": [\"1\", \"2\", \"3\"]\n" "}";Map map JSON.parseObject(jsoNStr,Map.class);2.Object轉為JsonArray(得保證obj是個Array數組&#x…

關于固件

固件(Firmware)就是寫入EROM或EPROM(可編程只讀存儲器)中的程序&#xff0c;通俗的理解就是“固化的軟件”&#xff0c;臺港澳稱為“韌體”。更簡單的說&#xff0c;固件就是BIOS的軟件&#xff0c;但又與普通軟件完全不同&#xff0c;它是固化在集成電路內部的程序代碼&#x…

React-Native 指定模擬器RUN-IOS

react-native run-ios --simulator "iPhone 7” 轉載于:https://www.cnblogs.com/smzd/p/10185263.html

vue和element-ui使用

上一篇已經創建好一個vue項目。https://mp.csdn.net/postedit/80926242 這一篇主要是創建一個vue項目并結合餓了么框架element-ui。 1.先創建vue項目&#xff0c;我準備把項目放在e盤下&#xff1a;E:\Work\RegisterProject&#xff1b; 命令行進入這個目錄&#xff1a; 創…

javaweb學習6——自定義標簽

聲明&#xff1a;本文只是自學過程中&#xff0c;記錄自己不會的知識點的摘要&#xff0c;如果想詳細學習JavaWeb&#xff0c;請到孤傲蒼狼博客學習&#xff0c;JavaWeb學習點此跳轉 本文鏈接&#xff1a;https://www.cnblogs.com/xdp-gacl/p/3916946.html https://www.cnblogs…

goland 實用鍵

代碼補全 option command v轉載于:https://www.cnblogs.com/smzd/p/10313417.html

關于Mysql java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)的問題...

問題所在&#xff1a; 1.連接數據庫一個是密碼是否正確&#xff0c; 2.driver是否對&#xff0c; 3.有么有jar包沖突&#xff0c;轉載于:https://www.cnblogs.com/java-123/p/9403412.html

vscode配置vue環境

一、安裝VSCode、NodeJS VSCode&#xff1a;https://code.visualstudio.com/ NodeJS&#xff1a;https://nodejs.org/en/ 二、打開VSCode&#xff0c;安裝常用插件 如圖所示&#xff08;安裝后重新加載即可&#xff09;: 三、項目中添加.vscode文件夾&#xff0c;文件夾中添…