python遍历数组的两种方法
2018-01-12 15:28
525 查看
python遍历数组的两种方法
第一种,最常用的,通过for in遍历数组
[cpp] view
plain copy
colours = ["red","green","blue"]
for colour in colours:
print colour
# red
# green
# blue
下面的方法可以先获得数组的长度,然后根据索引号遍历数组,同时输出索引号
[cpp] view
plain copy
colours = ["red","green","blue"]
for i in range(0, len(colours)):
print i, colour[i]
# 0 red
# 1 green
# 2 blue
第一种,最常用的,通过for in遍历数组
[cpp] view
plain copy
colours = ["red","green","blue"]
for colour in colours:
print colour
# red
# green
# blue
下面的方法可以先获得数组的长度,然后根据索引号遍历数组,同时输出索引号
[cpp] view
plain copy
colours = ["red","green","blue"]
for i in range(0, len(colours)):
print i, colour[i]
# 0 red
# 1 green
# 2 blue
相关文章推荐
- python遍历数组的两种方法
- python遍历数组的两种方法
- python遍历数组的两种方法
- Python遍历目录文件的两种方法
- Python数组遍历的简单实现方法小结
- 矩阵或多维数组两种常用实现方法 - python
- 遍历数组的两种方法
- python两种遍历字典(dict)的方法比较
- Python中文件遍历的两种方法
- python遍历目录的两种方法
- nodejs 遍历数组的两种方法
- python数据结构——层次遍历的两种方法
- python遍历数组的方法小结
- python两种遍历字典(dict)的方法比较
- Python遍历文件夹的两种方法比较
- PHP递归遍历多维数组的两种方法
- javascript遍历数组的两种方法
- Python中文件遍历的两种方法
- python数组遍历三种实用方法
- PHP 遍历数组的两种方法