Examples of three traversal methods of Python List (List), pythonlist
Python Traversal
I recently learned the python language and felt that it has greatly improved my work efficiency. I don't want to talk about it here. I just paste the Code 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:
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.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!