赞
踩
- # [] for i in range(3) 其中3是可以遍历的对象,遍历为0,1,2。而i是遍历的值。[]是遍历值的类型。
- # lists = [[] for i in range(3)] 相当于list[]中创建了三个列表,即list[[],[],[]]
- lists = [[] for i in range(3)] # 创建的是三行多列的二维列表
- for i in range(3):
- lists[0].append(i)
- for i in range(5):
- lists[1].append(i)
- for i in range(7):
- lists[2].append(i)
- print("lists is:", lists)
- sum_0 = 0
- for i in range(len(lists)): #得到行长度,并遍历
- for j in range(len(lists[i])): #得到列长度,并遍历
- print(lists[i][j])
- sum_0 += lists[i][j]
- print("The sum_0 of Lists:", sum_0)
使用句柄遍历列表
- sum_1 = 0
- for i in lists:
- for j in i:
- sum_1 += j
-
- print("The sum_1 of Lists:", sum_1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。