生成器
有時候沛善,序列或集合內(nèi)的元素的個數(shù)非常巨大,如果全制造出來并放入內(nèi)存,對計算機(jī)的壓力是非常大的。比如,假設(shè)需要獲取一個10**20次方如此巨大的數(shù)據(jù)序列拆檬,把每一個數(shù)都生成出來,并放在一個內(nèi)存的列表內(nèi)妥凳,這是粗暴的方式竟贯,有如此大的內(nèi)存么?如果元素可以按照某種算法推算出來逝钥,需要就計算到哪個屑那,就可以在循環(huán)的過程中不斷推算出后續(xù)的元素,而不必創(chuàng)建完整的元素集合艘款,從而節(jié)省大量的空間持际。在Python中,這種一邊循環(huán)一邊計算出元素的機(jī)制哗咆,稱為生成器:generator蜘欲。
生成生成器:
g = (x * x for x in range(1, 4))
g
<generator object <genexpr> at 0x1022ef630>
可以通過next()函數(shù)獲得generator的下一個返回值,這點(diǎn)和迭代器非常相似:
next(g)
1
next(g)
4
next(g)
9
next(g)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
next(g)
StopIteration
------------------------------------------------
但更多情況下晌柬,我們使用for循環(huán)姥份。
for i in g:
print(i)
除了使用生成器推導(dǎo)式郭脂,我們還可以使用yield關(guān)鍵字。
def createNums():
print("----func start------")
a,b = 0,1
for i in range(5):
# print(b)
print("--1--")
yield b
print("--2--")
a,b = b,a+b # a,b = 1, 1 a,b = 1,2
print("--3--")
print("----func end------")
g= createNums()
next(g) # 如果想得到y(tǒng)ield的值,可以打印next(g)
在 Python中澈歉,使用yield返回的函數(shù)會變成一個生成器(generator)展鸡。 在調(diào)用生成器的過程中,每次遇到y(tǒng)ield時函數(shù)會暫停并保存當(dāng)前所有的運(yùn)行信息埃难,返回yield的值莹弊。并在下一次執(zhí)行next()方法時從當(dāng)前位置繼續(xù)運(yùn)行。
# 斐波那契函數(shù)
def fibonacci(n):
a = 0
b = 1
counter = 0
while True:
if counter > n:
return
yield a # yield讓該函數(shù)變成一個生成器
a, b = b, a + b
counter += 1
fib = fibonacci(10) # fib是一個生成器
print(type(fib))
for i in fib:
print(i, end=" ")
生成器是可以循環(huán)的,相比next來說,for循環(huán)更友好
a = createNums()
這兩種取值方式是一樣的N谐尽O渌丁!
a.__next__()
next(a)
for i in a:
print(i)
send
def test():
i = 0
while i<5:
temp = yield i
print(temp)
i+=1
t = test()
next(t)
next(t)
t.send("juran")
next(t)
--------------------------------------------
t = test()
t.send("juran")
Traceback (most recent call last):
File "/Users/binbin/Desktop/Python/demo.py", line 179, in <module>
t.send("juran")
TypeError: can't send non-None value to a just-started generator
如何解決這個錯誤?
> next(t)
t.send("juran")
> send(None)
生成器的應(yīng)用
實(shí)現(xiàn)多任務(wù)
def test1():
while True:
print("--1--")
yield None
def test2():
while True:
print("--2--")
yield None
t1 = test1()
t2 = test2()
while True:
next(t1)
next(t2)
(寫在最后悟衩,由于以后每天晚上九點(diǎn)半之后會更新Python基礎(chǔ)的知識點(diǎn),記得來看哦Kò荨)
此文來源于微博和今日頭條:邏二妞座泳,轉(zhuǎn)載請注明出處,謝謝