Python List (List) Three traversal (sequence number and value) methods, pythonlist
Three methods are used to traverse the serial numbers and values in the list:
I recently learned the python language and felt that it has greatly improved my work efficiency. I wrote this blog on Valentine's Day. I will not talk about it here, but I will post it directly.
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 if _ name _ = '_ main __': 4 list = ['html', 'js', 'css ', 'python'] 5 6 # method 1 7 print 'traverse list Method 1: '8 for I in list: 9 print ("No.: % s value: % s" % (list. index (I) + 1, I) 10 11 print '\ n traversal list Method 2: '12 # method 213 for I in range (len (list )): 14 print ("No.: % s value: % s" % (I + 1, list [I]) 15 16 # method 317 print '\ n traversal list method 3: '18 for I, val in enumerate (list): 19 print ("No.: % s value: % s" % (I + 1, val )) 20 21 # method 322 print '\ n traverse list method 3 (set the start position of traversal and only change the start sequence number): '23 for I, val in enumerate (list, 2): 24 print ("No.: % s value: % s" % (I + 1, val ))
The result after running the code is shown in:
Here we will introduce the enumerate () method and view it through the help () function. The query results are as follows:
The second parameter of the enumerate () function only changes the starting value of the sequence number and does not change other parameters.