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

Source: Internet
Author: User

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

This function converts an Iterated object to an enumerated object. Iterable is an iterative parameter, such as a list, array, or dictionary object. start is the start value of enumeration. The default value is from 0. The implementation principle of this function is as follows: Get a value from the method _ next _ () of the iteration object, and then count the parameter start. each item is increased by 1, generate a tuple and return it.

The implementation principle of this function can be expressed in the following code:

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 'print (list (enumerate (s, 1 )))

The output is as follows:

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

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

[(2, 'shenzhen '), (3, 'zhuhai'), (4, 'guangzhou ')]

[(1, 'shen '), (2, 'shen'), (3, 'Yes'), (4, '1'), (5, 'tag'), (6, 'high'), (7, 'core'), (8, 'tech'), (9, '), (10, 'super'), (11, 'level'), (12, 'city'), (13, 'city'), (14, ','), (15, people), (16, 'login'), (17, 'login'), (18, 'you'), (19, '1'), (20, '8'), (21, '0'), (22, '0'), (23, 'wan'), (24, 'people')]



Cai junsheng QQ: 9073204 Shenzhen

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.