python遍历数组的方法小结

WBOY
Release: 2016-06-06 11:15:31
Original
1651 people have browsed it

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

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

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

colours = ["red","green","blue"]
for colour in colours:
  print colour
# red
# green
# blue
Copy after login

下面的方法可以先获得数组的长度,然后根据索引号遍历数组,同时输出索引号

colours = ["red","green","blue"]
for i in range(0, len(colours)):
  print i, colour[i]
# 0 red
# 1 green
# 2 blue
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!