Python Practice › NumPy Programs › Program 64
Split into 2, 3 and 5 elements
Write a NumPy program to split 10 elements into arrays of 2, 3 and 5 elements.
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
[1 2] [3 4 5] [ 6 7 8 9 10]
Approach
np.splitwith the split positions [2, 5].
Solution
import numpy as npfor part in np.split(np.arange(1, 11), [2, 5]): print(part)Output
[1 2] [3 4 5] [ 6 7 8 9 10]
Line by line
Line 2
Splitting at indexes 2 and 5 gives sizes 2, 3 and 5.
Learn the concept
New to this topic? Read the lesson, then come back.