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

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python生成器創(chuàng)建的方法整理

1、推導式的方法,只需將列表生成的[]改為()

成都創(chuàng)新互聯(lián)公司專注于長興網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經驗。 熱誠為您提供長興營銷型網(wǎng)站建設,長興網(wǎng)站制作、長興網(wǎng)頁設計、長興網(wǎng)站官網(wǎng)定制、小程序制作服務,打造長興網(wǎng)絡公司原創(chuàng)品牌,更為您提供長興網(wǎng)站排名全網(wǎng)營銷落地服務。

創(chuàng)建生成器的方法有很多。

In [26]: L = [num * 2 for num in range(5)]
 
In [27]: L
Out[27]: [0, 2, 4, 6, 8]
 
In [28]: G = (num * 2 for num in range(5))
 
In [29]: G
Out[29]:  at 0x000001D62EA28248>

2、next() 函數(shù)

In [30]: next(G)
Out[30]: 0
 
In [31]: next(G)
Out[31]: 2
 
In [32]: next(G)
Out[32]: 4
 
In [33]: next(G)
Out[33]: 6
 
In [34]: next(G)
Out[34]: 8
 
In [35]: next(G)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
 in 
----> 1 next(G)
 
StopIteration:

3、for循環(huán)與list,因為G已經迭代到了ipython測試的最后,所以需要重建G,否則就沒有數(shù)據(jù)了。

In [38]: G = (num * 2 for num in range(5))
 
In [39]: for i in G:
    ...:     print(i)
    ...:
0
2
4
6
8
 
In [40]: list(G)
Out[40]: []
 
In [41]: G = (num * 2 for num in range(5))
 
In [42]: list(G)
Out[42]: [0, 2, 4, 6, 8]

以上就是python生成器創(chuàng)建的方法整理,希望對大家有所幫助。更多編程基礎知識學習:python學習網(wǎng)

本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。


分享標題:創(chuàng)新互聯(lián)Python教程:python生成器創(chuàng)建的方法整理
地址分享:http://m.5511xx.com/article/dhdeoje.html