Python indexing and slicing on an array
kodingwindow@kw:~$ python3 ... >>> from array import * >>> arr = array("i", [40, 96, 24, 55, 78, -111]) >>> arr array('i', [40, 96, 24, 55, 78, -111]) >>> for i in range(len(arr)): ... print(arr[i]) ... 40 96 24 55 78 -111 >>> arr[3:] array('i', [55, 78, -111]) >>> arr[-3:] array('i', [55, 78, -111]) >>> arr[:3] array('i', [40, 96, 24]) >>> arr[:] array('i', [40, 96, 24, 55, 78, -111])
>>> arr[0:6:1] array('i', [40, 96, 24, 55, 78, -111]) >>> arr[0:6:2] array('i', [40, 24, 78]) >>> arr[0:6:3] array('i', [40, 55]) >>> arr[::1] array('i', [40, 96, 24, 55, 78, -111]) >>> arr[::2] array('i', [40, 24, 78]) >>> arr[::3] array('i', [40, 55]) >>> arr[::-1] array('i', [-111, 78, 55, 24, 96, 40])
What Next?
Python NumPy Arrays
Python to create a view() and copy() of NumPy arrays
Python NumPy Arrays Comparison
Advertisement