Python is a list listing the packet / block

For example: the list: [1,2,3,4,5,6,7,8,9]
be divided into three groups according to the order of subscripts: [1,2,3] [4,5,6] [7,8 , 9]
or divided into 5 groups: [1, 2] [3, 4] [5,6] [7, 8] [9]

Solution:

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

 

# Set function 
# number n is the listTemp bisecting each list a listing of 
DEF FUNC (listTemp, n):
     for I in Range (0, len (listTemp), n):
         the yield listTemp [I: I + n]

 

Guess you like

Origin www.cnblogs.com/shenxiaolin/p/11268817.html