python 遍歷數組有兩種方法,一種是使用for in 來遍歷數組,一種是先獲得數組的長度,然后根據索引號遍歷數組,同時輸出索引號。
for in 遍歷數組方法:
colours = ["red","green","blue"]
for colour in colours:
print colour
red
green
blue
遍歷數組方法二:
colours = ["red","green","blue"]
for i in range(0, len(colours)):
print i, colour[i]
0 red
1 green
2 blue