Three ways to iterate through the list of numbers and values:
Recently learn the language of Python, feel its work efficiency has a great improvement, special on Valentine's Day wrote this blog, the following nonsense say, direct sticker code
#!/usr/bin/env python#-*-coding:utf-8-*-if __name__ = = ' __main__ ': list = [' html ', ' JS ', ' css ', ' python '] # method 1 print ' traversal list Method 1: ' For i in list: print ("Ordinal:%s value:%s"% (List.index (i) + 1, i)) print ' \ n Traverse list Method 2: '
# Method 2 for I in range (len list): print ("Ordinal:%s value:%s"% (i + 1, list[i]) # Method 3 print ' \ n Traverse list Method 3: ' for-I, Val in enumerate (list): print ("Ordinal:%s value:%s"% (i + 1, val)) # Method 3 print ' \ n Traverse list Method 3 (set traverse on Initial position, changed only the starting sequence number): ' For I, Val in enumerate (list, 2): print ("Ordinal:%s value:%s"% (i + 1, val))
The results after running the code are as follows:
Introduce the enumerate () method here and look at the Help () function to see the following results:
Finally, the second parameter of the enumerate () function only changes the starting value of the ordinal, and does not change the other stuff.