Enumerate () is a built-in function in Python, using the method
Enumerate (X, [start=0])
The parameter X in a function can be an iterator (iterator) or a sequence, start is the starting count, and the default starts at 0.
X can be a dictionary, or it can be a sequence.
>>> a = {1:1, 2:2, 3:3}
>>> for I, item in enumerate (a):
... Print I, item
ouput:
0 1
1 2
2 3
>>> B = [1,2,3,4,5,6]
>>> for I, item in enumerate (b):
... Print I, item
ouput: 0 1 1 2 2 3 3 4 4 5 5 6
>>> for I, item in Enumerat E (b, start=10): ... Print I, item
ouput:
1
2
3
4
5
15 6
Supplementary: Number of rows of statistical files
Learning from the Enumerate Usage Summary blog, enumerate can also be used to count the number of rows in a file to handle larger files.
Count = 0
file_count = open (filepath, ' R ')
for index, line in enumerate (file_count):
count = 1
Print Count