python enumerate 函數用法

來源:互聯網
上載者:User

標籤:summer   tor   res   字典   art   int   use   print   other   

enumerate字典上是枚舉、列舉的意思。

 C語言中關鍵字enum也是enumerate的縮寫。 python中enumerate方法,返回一個enumerate類型。參數一般是可以遍曆的的東西,比如列表,字串什麼的。 python文檔中是這麼說的: enumerate(sequence, [start=0])Return an enumerate object. sequence must be a sequence, an iterator, or some other object which sup-ports iteration. The next() method of the iterator returned by enumerate() returns a tuple containinga count (from start which defaults to 0) and the corresponding value obtained from iterating over iter-able. enumerate() is useful for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2,seq[2]), ....  For example:>>> for i, season in enumerate([’Spring’, ’Summer’, ’Fall’, ’Winter’]):... print i, season0 Spring1 Summer2 Fall3 Winter 我試了一下,如果那句迴圈改成for  season  in  [’Spring’, ’Summer’, ’Fall’, ’Winter’]: 其實也是可以的,只是enumerate會返回一個有索引的東西,就是前邊的那個i,那個i從0開始,每一項加一。

python enumerate 函數用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.