Segmenting list in python and saving it as a method of array type

  • 2021-07-18 08:19:40
  • OfStack

As shown below:


list=[1,2,3,4,5,6,7,8,9,0,11,0,13,14,15,16,17,18,19,20]
# Put list Divided into lengths of 5 Adj. 4 Segment 
for j in range(0,len(list),5):
  matrix.append(list[j:j+5])
matrix=np.array(matrix)# Turn array Type 
print matrix[0]# Output number 1 Segment 

Results:


[[ 1 2 3 4 0]
 [ 6 7 8 0 0]
 [11 0 13 14 15]
 [16 0 18 19 20]]

Related articles: