This article mainly introduces three methods of traversing Python List (List) (sequence number and value. It has good reference value. let's take a look at the following three methods 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.
#! /Usr/bin/env python #-*-coding: UTF-8-*-if _ name _ = '_ main _': list = ['HTML ', 'js', 'css ', 'Python'] # Method 1 print Method 1: 'For I in list: print ("SN: % s value: % s "% (list. index (I) + 1, I) print '\ n traversal list Method 2:' # Method 2 for I in range (len (list): print ("No: % s value: % s "% (I + 1, list [I]) # method 3 print '\ n traversal list method 3:' for I, val in enumerate (list): print ("No.: % s value: % s" % (I + 1, val )) # method 3 print '\ n traverse list method 3 (set the start position of traversal and only change the start sequence number):' for I, val in enumerate (list, 2 ): print ("SN: % s value: % s" % (I + 1, val ))
The result after running the code is shown in:
The second parameter of the enumerate () function only changes the starting value of the sequence number and does not change other parameters.
For more information about how to use Python to traverse the serial numbers and values in the list (3), refer to PHP!