Python Practice › NumPy Programs › Program 64

Split into 2, 3 and 5 elements

EasyNumPy Programs · 64 of 100

Write a NumPy program to split 10 elements into arrays of 2, 3 and 5 elements.

Advertisement
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
[1 2]
[3 4 5]
[ 6  7  8  9 10]

Approach

  1. np.split with the split positions [2, 5].

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs