A brief summary of the usage of the enumerate function in Python

A brief summary of the usage of the enumerate function in Python

It is a built-in function of python

  • enumerate means: enumerate, enumerate

  • For an iterable object, enumerate turns it into an index sequence

    E.g

seq = ['one', 'two', 'three']
list(enumerate(seq))
out:
[(0, 'one'), (1, 'two'), (2, 'three')]

Pass a readable object to the enumerate function, and he returns an object of enumerated type. After converting it into a list, the output element is a list of the original ancestor ('serial number', corresponding element). In addition, if you want to make the index not start from 0, you can pass a parameter when using it, such as the following figure:

>>> e=(enumerate(s,start=1))
>>> for i in e:
...     print(i)
...
(1, 'h')
(2, 'e')
(3, 'l')
(4, 'l')
(5, 'o')

Usage is roughly like this

Guess you like

Origin blog.csdn.net/weixin_45717055/article/details/112723479