国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python迭代器實例簡析

2020-02-23 05:50:59
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了python迭代器的簡單用法,分享給大家供大家參考。具體分析如下:

生成器表達式是用來生成函數(shù)調(diào)用時序列參數(shù)的一種迭代器寫法

生成器對象可以遍歷或轉化為列表(或元組等數(shù)據(jù)結構),但不能切片(slicing)。當函數(shù)的唯一的實參是可迭代序列時,便可以去掉生成器表達式兩端>的圓括號,寫出更優(yōu)雅的代碼:

>>>> sum(i for i in xrange(10)) 45

sum聲明:

sum(iterable[, start])
Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and are not allowed to be strings. The fast, correct way to concatenate a sequence of strings is by calling ''.join(sequence). Note that sum(range(n), m) is equivalent to reduce(operator.add, range(n), m) To add floating point values with extended precision, see math.fsum().

參數(shù)要求傳入可迭代序列,我們傳入一個生成器對象,完美實現(xiàn)。

注意區(qū)分下面代碼:

上面的j為生成器類型,下面的j為list類型:

j = (i for i in range(10)) print j,type(j) print '*'*70  j = [i for i in range(10)] print j,type(j) 

結果:

<generator object <genexpr> at 0x01CB1A30> <type 'generator'>**********************************************************************[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] <type 'list'>

希望本文所述對大家Python程序設計的學習有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 涿鹿县| 航空| 新龙县| 罗山县| 磐安县| 观塘区| 吐鲁番市| 天峨县| 藁城市| 噶尔县| 公主岭市| 眉山市| 育儿| 台南县| 门头沟区| 尚志市| 玛沁县| 玉溪市| 阳江市| 鱼台县| 都匀市| 鸡西市| 泾川县| 宜黄县| 伊吾县| 新巴尔虎右旗| 双桥区| 博乐市| 灵寿县| 科技| 成安县| 慈利县| 得荣县| 巢湖市| 涞源县| 丹阳市| 安阳县| 宜宾县| 南乐县| 成都市| 衢州市|