python遍曆數組的兩種方法

  • 2020 年 1 月 13 日
  • 筆記

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