Python: 列表的两种遍历方法

 1 方法一:以列表中元素的下标进行访问
 2 def traverse1(list1):
 3     for i in range(len(list1)):
 4         print(list1[i], end=' ')
 5     print()
 6 
 7 
 8 方法二:以列表中的每个元素进行访问
 9 def traverse2(list1):
10     for enum in list1:
11         print(enum, end=' ')
12     print()
13 
14 
15 if __name__ == '__main__':
16     list1 = ['a', 'b', 'c', 'd']
17     traverse1(list1)
18     traverse2(list1)

输出结果:

原文地址:https://www.cnblogs.com/huiAlex/p/8029389.html