1. 程式人生 > >[python] 遍歷列表四種方法

[python] 遍歷列表四種方法

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 19 19:51:08 2018

@author: yhl
"""

a=[10,23,45,87]
print '1:'
for val in a:
    print val,
      
print '\n\n2:'
for index,val in enumerate(a):
    print index,val

print '\n3:'
for i in range(len(a)):
    print i,a[i]
    
print '\n4:' #使用iter()
for val in iter(a):
    print val,

輸出:

1:
10 23 45 87 

2:
0 10
1 23
2 45
3 87

3:
0 10
1 23
2 45
3 87

4:
10 23 45 87