Reprinted from: Http://blog.csdn.net/churximi/article/details/51648388enumerate () description
- Enumerate () is a python built-in function
- Enumerate are enumerated and enumerated in the dictionary.
- For an iterative (iterable)/Ergodic object (such as a list, string), enumerate makes it an index sequence that can be used to obtain both the index and the value
- Enumerate multiple for counting in a for loop
-
For example, for a seq, get:
(0, seq[0]), (1, seq[1]), (2, seq[2])
- Enumerate () Returns a enumerate object
Enumerate () use
- If you want to traverse the index and iterate through the elements on a list, you can first write this:
List1 = [" this " " is " "" one "" Test " ] for in range (Len (list1)): print I, List1[i]
- The above method is somewhat cumbersome, and the use of enumerate () will be more direct and graceful:
List1 = [ " this ", " is ", " one ", " test " ] for Index, item in enumerate (LIST1): Print index, item >>>0 this 1 is 2 a 3 test
The
- enumerate can also receive a second parameter that specifies the index starting value, such as:
List1 = [ " this ", " is ", " one ", " test " ] for Index, item in Enumerate (list1, 1 >>>1 This 2 3 a 4 test
Add
If you want to count the number of lines in a file, you can write:
' R '). ReadLines ())
You can use enumerate ():
0 for in Enumerate (open (filepath,'r')): 1
Python-enumberate function