python如何遍历所有数组

作者: web 发布时间: 2022-10-28 浏览: 2511 次 编辑

python如何遍历所有数组?下面给大家介绍两种python遍历数组的方法:

第一种,最常用的,通过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如何遍历所有数组的详细内容