日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python生成器函數(shù)

Python 提供了一個(gè)生成器來創(chuàng)建自己的迭代器函數(shù)。 生成器是一種特殊類型的函數(shù),它不返回單個(gè)值,而是返回一個(gè)包含一系列值的迭代器對象。 在生成器函數(shù)中,使用yield語句,而不是返回語句。 下面是一個(gè)簡單的生成器函數(shù)。

創(chuàng)新互聯(lián)建站是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站備案、服務(wù)器租用、域名申請、軟件開發(fā)、小程序開發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個(gè)面向全國乃至全球的業(yè)務(wù)窗口:建站歡迎來電:18982081108

Example: Generator Function

def mygenerator():
    print('First item')
    yield 10

    print('Second item')
    yield 20

    print('Last item')
    yield 30 

在上面的例子中,mygenerator()函數(shù)是一個(gè)生成器函數(shù)。它使用yield而不是 return 關(guān)鍵字。 因此,這將在每次調(diào)用yield關(guān)鍵字時(shí)返回該值。但是,您需要為此函數(shù)創(chuàng)建一個(gè)迭代器,如下所示。

Example: next()

>>> gen = mygenerator() 
>>> next(gen) 
First item 
10                      
>>> next(gen) 
Second item 
20                      
>>> next(gen) 
Last item 
30 

生成器函數(shù)不能包含return關(guān)鍵字。如果包含它,那么它將終止函數(shù)。 yieldreturn的區(qū)別在于yield返回值并暫停執(zhí)行,同時(shí)保持內(nèi)部狀態(tài),而return語句返回值并終止函數(shù)的執(zhí)行。

以下生成器函數(shù)包含 return 關(guān)鍵字。

Example: return in Generator Function

def mygenerator():
    print('First item')
    yield 10

    return

    print('Second item')
    yield 20

    print('Last item')
    yield 30 

現(xiàn)在,如下所示執(zhí)行上述功能。

Example: Generator Function

>>> gen = mygenerator() 
>>> next(gen) 
First item 
10                      
>>> next(gen) 
Traceback (most recent call last):
        File "", line 1, in 

                it.__next__()
                StopIteration 

如您所見,上面的生成器在獲取第一項(xiàng)后停止執(zhí)行,因?yàn)?return 關(guān)鍵字是在yield獲取第一項(xiàng)后使用的。

用于具有生成器功能的循環(huán)

生成器函數(shù)也可以使用 for循環(huán)。

Example: Use For Loop with Generator Function

def get_sequence_upto(x):
    for i in range(x):
        yield i 

如上圖所示,get_sequence_upto函數(shù)使用了yield關(guān)鍵字。 發(fā)電機(jī)的調(diào)用就像正常功能一樣。 然而,當(dāng)遇到yield關(guān)鍵字時(shí),其執(zhí)行被暫停。這將迭代器流的第一個(gè)值發(fā)送到調(diào)用環(huán)境。但是,局部變量及其狀態(tài)保存在內(nèi)部。

上面的生成器函數(shù)get_sequence_upto()可以如下調(diào)用。

Example: Calling Generator Function

>>> seq = get_sequence_upto(5) 
>>> next(seq) 
0                      
>>> next(seq) 
1                      
>>> next(seq) 
2                      
>>> next(seq) 
3                      
>>> next(seq) 
4                                                          
>>> next(seq)                                     
Traceback (most recent call last):
        File "", line 1, in 

                it.__next__()
                StopIteration 

當(dāng)向迭代器對象發(fā)出 next() 時(shí),該函數(shù)恢復(fù)。當(dāng)next()遇到StopIteration錯(cuò)誤時(shí),該功能最終終止。

在下面的例子中,函數(shù)square_of_sequence()充當(dāng)一個(gè)生成器。它在每次調(diào)用 next() 時(shí)連續(xù)產(chǎn)生一個(gè)數(shù)字的平方。

Example:

def square_of_sequence(x):
    for i in range(x):
        yield i*i 

下面的腳本顯示了如何調(diào)用上面的生成器函數(shù)。

gen=square_of_sequence(5)
while True:
    try:
        print ("Received on next(): ", next(gen))
    except StopIteration:
        break 

上面的腳本使用try..except塊來處理StopIteration錯(cuò)誤。一旦捕捉到StopIteration錯(cuò)誤,它將中斷 while循環(huán)。

Output

Received on next(): 0
Received on next(): 1
Received on next(): 4
Received on next(): 9
Received on next(): 16 

我們可以使用 for循環(huán)遍歷生成器上的元素。在這種情況下,next()函數(shù)被隱式調(diào)用,StopIteration也被自動(dòng)處理。

Example: Generator with the For Loop

squres = square_of_sequence(5)
for sqr in squres:
    print(sqr) 

Output

0
1
4
9
16 

*Note:*One of the advantages of the generator over the iterator is that elements are generated dynamically. Since the next item is generated only after the first is consumed, it is more memory efficient than the iterator. *## 生成器表達(dá)式

Python 還提供了一個(gè)生成器表達(dá)式,這是定義簡單生成器函數(shù)的一種更短的方式。生成器表達(dá)式是匿名生成器函數(shù)。以下是square_of_sequence()函數(shù)的生成器表達(dá)式。

Example: Generator Expression

>>> squres = (x*x for x in range(5))
>>> print(next(squre))              
0                                            
>>> print(next(squre))              
1                                            
>>> print(next(squre))              
4                                            
>>> print(next(squre))              
9                                            
>>> print(next(squre))              
16 

在上面的例子中,(x*x for x in range(5))是一個(gè)生成器表達(dá)式。表達(dá)式的第一部分是yield值,第二部分是帶有集合的 for循環(huán)。

生成器表達(dá)式也可以在函數(shù)中傳遞。它應(yīng)該不帶括號傳遞,如下所示。

Example: Passing Generator Function

>>> import math
>>> sum(x*x for x in range(5)) 
30 

在上面的例子中,一個(gè)沒有括號的生成器表達(dá)式被傳遞到內(nèi)置函數(shù)sum中。***


網(wǎng)頁名稱:Python生成器函數(shù)
鏈接地址:http://www.5511xx.com/article/dhhppee.html