Python 中把一个list 列表分组/分块

比如:将list:[1,2,3,4,5,6,7,8,9]
按照下标顺序分成3组:[1,2,3] [4,5,6] [7,8,9]
或分成5组:[1,2,] [3, 4] [5,6] [7, 8] [ 9 ]

解决办法:

a=[1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(a),3):
    b=a[i:i+3]
    print b
# 设置函数
# listTemp 为列表 平分后每份列表的的个数n
def func(listTemp, n):
    for i in range(0, len(listTemp), n):
        yield listTemp[i:i + n]

猜你喜欢

转载自www.cnblogs.com/shenxiaolin/p/11268817.html