w3resource logo
Python Exercises

Python NumPy: Split an array of 14 elements into 3 arrays

Python NumPy: Array Object Exercise-61 with Solution

Write a Python program to split an array of 14 elements into 3 arrays, each of which has 2, 4, and 8 elements in the original order.
Sample array: (10,20,30), (40,50,60)

Sample Solution:-

Python Code:

import numpy as np
x = np.arange(1, 15)
print("Original array:",x)
print("After splitting:")
print(np.split(x, [2, 6]))

Sample Output:

Original array: [ 1  2  3  4  5  6  7  8  9 10 11 12 13 14]            
After splitting:                                                       
[array([1, 2]), array([3, 4, 5, 6]), array([ 7,  8,  9, 10, 11, 12, 13,
 14])] 

Python Code Editor:

import numpy as np
x = np.arange(1, 15)
print("Original array:",x)
print("After splitting:")
print(np.split(x, [2, 6]))

Improve this sample solution and post your code through Disqus