Python3 迭代器,Python3代器

來源:互聯網
上載者:User

Python3 迭代器,Python3代器

 1 ''' 2 產生器都是迭代器,迭代器不一定是產生器 3 ''' 4  5 # list1 = [1,2,3,4,5] 6 # p1 = iter(list1)        # 等價於__iter__() 7 # print(p1) 8 # print(next(p1)) 9 # print(next(p1))10 # print(next(p1))11 # print(next(p1))12 13 14 '''15 迭代器必須滿足下列兩個條件:16 1.有iter方法17 2.有next方法18 '''19 20 '''21 for迴圈內部的三件事:22 1.調用可迭代對象的iter方法,返回一個迭代器對象23 2.調用迭代器對象的next方法24 3.處理StopIteration25 '''26 27 # for i in [5,6,7]:28 #     iter([5,6,7])29 30 # from collections import Iterator,Iterable       # 迭代器,迭代對象31 # print(isinstance([3,4],list))       # 判斷前一個參數是不是後邊參數寫出的type32 # print(isinstance(6,list))           # 一樣是True,否則是False33 # 34 # 35 # list2 = [1,2,3,4,5,6,7,8,9,10]36 # p2 = iter(list2)37 # print(p2)38 # print(isinstance(list2,list))       # 是否是列表39 # print(isinstance(list2,Iterable))   # 是否是迭代對象40 # print(isinstance(list2,Iterator))   # 是否是迭代器41 # print(isinstance(p2,Iterator))      # 是否是迭代器

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.