列表的迭代最好使用for循环

举例:

遍历列表的中元素

num_list = [1,2,3,4,5,6,7,8,9]

1,使用for循环

for i  in num_list:

  print(i)

即可;

2,使用while循环

首先必须考虑状态信息,这就要求使用一个计数标识

count = 0

while count < len(num_list):

  print(num_list[count])

  count +=1

以上for与while语句完成的工作是一样的,显然for循环更简洁。

猜你喜欢

转载自www.cnblogs.com/zujianke/p/9713265.html