Python standard library: Built-in function enumerate (iterable, start=0)

Source: Internet
Author: User

This function converts an iterative object to an enumeration object. iterable is an iterative parameter, such as a list, an array, a dictionary, and other objects; Start is the starting value of the enumeration, which is the default from 0 start. The principle of this function is to obtain a value from the method __next__ () of the iterative object, and then Start counting the parameter start, adding 1 per item , a tuple is generated to return.

This function implements the principle, basically can use the following code to represent:

def enumerate (sequence, start=0):

n = Start

For Elem in sequence:

Yield N, Elem

n + = 1

Let's take a look at the example below:

#enumerate () L = [' A ', ' B ', ' C ']print (List (Enumerate (L))) T = (' happy ', ' happy ', ' happy ') print (list (enumerate (t))) d = {' Shenzhen ': 1, ' Guangzhou ': 2, ' Zhuhai ': 3}print (List (Enumerate (d, 2))) s = ' Shenzhen is a high-tech super city with a population of 18 million people ' Print (list (enumerate (S, 1)))

The resulting output is as follows:

[(0, ' a '), (1, ' B '), (2, ' C ')]

[(0, ' happy ' ), (1, ' happy ') , (2, ' happy ')]

[(2, ' Shenzhen ' ), (3, ' Zhuhai '), (4, ' guangzhou ')]

[(1, 'Deep'), (2, 'Chun'), (3, 'is a'), (4, 'a'), (5, 'a'), (6, 'High'), (7, 'Section'), (8, 'Technology'), (9, 'of the'), (Ten, 'Super'), (one, '-Level'), (City'), (inCity'), (+, ','), (people'), (+, 'Mouth'), (+, 'hold'), (have a'), (+, ' 1 ') , (+, ' 8 '), (+, ' 0 '), (+, ' 0 '), (atmillion'), (people')]



Cai Junsheng qq:9073204 Shenzhen

Python standard library: Built-in function enumerate (iterable, start=0)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.