a = [1,2,3,4,5,6,7,8,9,10,11]
step = 3
b = [a[i:i+step] for i in range(0,len(a),step)]
print(b)
>>> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]
a = [1,2,3,4,5,6,7,8,9,10,11]
step = 3
for i in range(0,len(a),step):
print(a[i:i+step])
>>> [1, 2, 3]
[4, 5, 6]
[7, 8, 9]
[10, 11]
注解:a[i:i+setp] 是对a列表进行切片,i是for语句取出的间隔值,i用于a切片的起点,i+setp是终点
感谢 Mr-Cat踏雪三郎给予提示,原网址:https://blog.csdn.net/mr_cat123/article/details/80584988