Let's try to keep it simple. If your length of the list is less than a multiple of 3. Just append it with 27s!. After that you can break it into 3 rows and remaining columns. Try this method -
- Append the list with the number of 27s that make the list's length divisible by 3
- Iterate and break list over chunks of length/3 = 8 in this case.
L=[16, 5, 14, 7, 21, 9, 14, 19, 27, 1, 18, 5,27, 15, 14, 5, 27, 20, 15, 27, 15, 14, 5]
L2 = L+[27]*(3-len(L)%3) #Append list
n = int(len(L2)/3) #Get number of cols
L3 = [L2[i:i+n] for i in range(0,len(L2),n)] #chunk list
L3
[[16, 5, 14, 7, 21, 9, 14, 19],
[27, 1, 18, 5, 27, 15, 14, 5],
[27, 20, 15, 27, 15, 14, 5, 27]]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…