Python实现列表均等分割(每间隔X分割)
代码
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)]
效果
>>> 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
b = [a[i:i+step] for i in range(0,len(a),step)]
>>> b
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11]]