Home  >  Article  >  Backend Development  >  python遍历数组的方法小结

python遍历数组的方法小结

WBOY
WBOYOriginal
2016-06-06 11:15:311531browse

本文实例总结了python遍历数组的方法。分享给大家供大家参考。具体分析如下:

下面介绍两种遍历数组的方法,一种是直接通过for in 遍历数组,另外一种是通过rang函数先获得数组长度,在根据索引遍历数组

第一种,最常用的,通过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

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn