python遍历数组的两种方法

第一种,最常用的,通过for in遍历数组

 

1
2
3
4
5
6
7
8
colours = ["red","green","blue"]
 
for colour in colours:
    print colour
 
# red
# green
# blue


第二种,先获得数组的长度,然后根据索引号遍历数组,同时输出索引号

 

 

1
2
3
4
5
6
7
8
colours = ["red","green","blue"]
 
for i in range(0, len(colours)):
    print i, colour[i]
 
# 0 red
# 1 green
# 2 blue
posted @ 2016-04-26 19:27  午时的海  阅读(11758)  评论(1编辑  收藏  举报