Python indexing and slicing on NumPy arrays
kodingwindow@kw:~$ python3 ... >>> from numpy import * >>> arr1 = array([40, 96, 24, 55, 78, -111]) >>> arr2 = array(['x', 'y', 'z']) >>> arr3 = array(["Orange", "Pineapple", "Banana", "Blueberry", "Grapes", "Fig"]) >>> for i in range(len(arr1)): ... print(arr[i]) ... 40 96 24 55 78 -111 >>> for s in range(len(arr3)): ... print(arr3[s]) ... Orange Pineapple Banana Blueberry Grapes Fig
>>> arr1[3:] array([ 55, 78, -111]) >>> arr1[-3:] array([ 55, 78, -111]) >>> arr1[:3] array([40, 96, 24]) >>> arr1[:] array([ 40, 96, 24, 55, 78, -111])
>>> arr1[0:6:1] array([ 40, 96, 24, 55, 78, -111]) >>> arr1[0:6:2] array([40, 24, 78]) >>> arr1[0:6:3] array([40, 55]) >>> arr1[::1] array([ 40, 96, 24, 55, 78, -111]) >>> arr1[::2] array([40, 24, 78]) >>> arr1[::3] array([40, 55]) >>> arr1[::-1] array([-111, 78, 55, 24, 96, 40]) >>> arr2[::-1] array(['z', 'y', 'x'], dtype='<U1')
What Next?
How to create arrays using the linspace() and logspace() functions
How to create arrays using the arange(), zeros(), and ones() functions
Python NumPy Multi-dimensional Arrays
Advertisement