l = [0,1,2,3,4,5,6,7,8,9,] #模拟一个大列表 def list_scan_iter(L): start = 0 #从0开始取值 while True: # 第一次:0,1,2,3 # 第二次:4,5,6,7 # 第三次:8,9 vals = L[start:start+4] #每次取4个值 start += 4 if not vals: #vals=[]时 return for val in vals: yield val ret = list_scan_iter(l) for i in ret: print(i)
结果:
0
1
2
3
4
5
6
7
8
9