Enumerate () is a python built-in function for python2.x and python3.x
Enumerate are enumerated and enumerated in the dictionary.
The enumerate parameter is an object that can be traversed/iterated (such as a list, a string)
The enumerate is used to get a count in the for loop, which can be used to get both the index and the value, which is required when the index and value values are needed enumerate
Enumerate () Returns a enumerate object
>>> LST = [1, 2, 3, 4, ten, 5]>>> Enumerate (LST)<enumerate object at 0x00000000032a3 990>
Use of Enumerate:
For example: LST = [1,2,3,4,5,6] is known to require output:
0,1
The
2,3
3,4
4,5
5,6
>>> lst = [1,2,3,4,5,6] for in Enumerate (LST): print (' %s,%s' % (index,value)) 0,11,22,33,44,55,6
#The specified index starts at 1>>> LST = [1,2,3,4,5,6]>>> forIndex,valueinchEnumerate (lst,1):Print('%s,%s'%(index,value))1,12,23,34,45,56,6#The specified index starts at 3>>> forIndex,valueinchEnumerate (lst,3):Print('%s,%s'%(index,value))3,14,25,36,47,58,6
Add:
If you want to count the number of lines in a file, you can write:
Count = Len (open (filepath, ' R '). ReadLines ())
This method is simple, but may be slow, and cannot even work when the file is larger.
You can use enumerate ():
Count = 0
For index, line in enumerate (open (filepath, ' R ')):
Count + = 1
Resources:
Python Enumerate usage Summary: http://blog.csdn.net/churximi/article/details/51648388
Enumerate usage in Python