The use of enumerate in Python is used for counting in the For loop, and this article shows you how to use the enumerate in Python in an instance form. Specifically as follows:
The enumerate parameter is a variable that can be traversed, such as a string, a list, and so on; The return value is the enumerate class.
The sample code looks like this:
Import string
s = string.ascii_lowercase
e = Enumerate (s)
print s
Print List (e)
The output is:
Abcdefghij
[(0, ' a '), (1, ' B '), (2, ' C '), (3, ' d '), (4, ' E '), (5, ' F '), (6, ' G '), (7, ' H '), (8, ' I '), (9, ' J ')]
Enumerate can be used when both index and value values are required.
Enumerate Application Example:
In this instance, line is a string containing 0 and 1, to find out the 1:
Method One:
def read_line (line):
sample = {}
n = len (line) for
I in range (n):
if line[i]!= ' 0 ':
sample[i] = i NT (Line[i]) return
sample
Method Two:
def xread_line (line): Return
((Idx,int (val) for IDX, Val-Enumerate (line) if Val!= ' 0 ')
print read_line (' 00011 10101 ')
Print List (xread_line (' 0001110101 '))
I believe this sample will help you to deepen your use of enumerate in Python.