Python Programming - Indexing And Slicing

You can learn about NumPy in Python Programs with Outputs helped you to understand the language better.

Python Programming – Indexing And Slicing

NumPy creates an appropriate scale index at the time of array creation. In order to access single or multiple items of an array, you need to pass an array of indexes in square brackets.
Indexing in a two-dimensional array is represented by a pair of values, where the first value is the index of the row and the second is the index of the column.
Integer array indexing: In this method, lists are passed for indexing for each dimension. One-to-one mapping of corresponding elements is done to construct a new arbitrary array.
Boolean array indexing : This method is used when you want to pick elements from array which satisfy some condition.

Example
Demo of indexing.

import numpy as np

array1d = np . array ( [ 1 , 2 , 3 , ,4 , 5 , 6] )
print ( array [ 0 ] ) # Get first value
print ( array [ -1 ] ) # Get last value
print ( array [ 3 ] ) # Get 4th value from first
print ( array [ -5 ] ) # Get 5th value from last

# Get multiple values
print(arrayld[[0, -1]])
print (“-” * 10)
array2d = np . array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(array2d)
print (“-” * 10)
print(array2d[0, 0] ) # Get first row first col
print(array2d[0, 1] ) # Get first row second col
print(array2d[0, 2] ) # Get first row third col
print(array2d[0, 1] ) # Get first row second col
print(array2d[1, 1] ) # Get second row second col
print(array2d[2, 1] ) # Get third row second col

RUN
>>>
1
6
4
2
[1 6]
– – – – – – – – –
[[1 2 3]
[4 5 6]
[789]]
– – – – – – – – –
1
2
3
2
5
8
>>>

Just like lists in Python, NumPy arrays can be sliced. Slicing allows extracting portions of an array or selection a subset of an existing array to generate new arrays. For slicing a sequence of numbers separated by colons (:) within square brackets. The syntax for per¬forming slicing :
<Arrayname>[<start>: <stop> : <step>]
When <start>, <stop> or <step> values are not specified then Python will assume their default values
as : start = 0, stop = dimension size, and step = 1.
A single item respective to that index is returned for only one parameter. If a: is inserted in front of it, all items from that index onwards will be extracted. If two parameters (with: between them) are used, items between the two indexes (not including the stop index) with default step one are sliced.

Example
Demo of slicing.

import numpy as np
arrayld = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(arrayld[4:]) # From index 4 to last index
print(arrayId[:4]) # From index 0 to 4 index
print(arrayld[4:7]) # From index 4(included) up to index 7(excluded)
print(arrayld[:-1]) # Excluded last element
print(arrayld[:-2]) # Up to second last index(negative index)
print(arrayld1]) # From last to first in reverse order(negative step)
print(arrayld[::-2]) # All odd numbers in reversed order
print(arrayld[-2::-2]) # All even numbers in reversed order
print(arrayld(::]) # All elementsRUN
>>>
[4 5 6 7 8 9]
[0 12 3]
[4 5 6]
[0 1234567 8]
[0 1 2 3 4 5 6 7]
[9 87654321 0]
[9 7 5 3 1]
[8 6 4 2 0]
[0 12345678 9]
>>>

Example
Demo of slicing and Indexing.

arr = np.array([[1, 2, 0, -4],
[4, 6, 7, 0],
[3, -7, 8, 5]])
# Slicing array
temp = arr [:2, ::2]
print (“Array with first 2 rows and alternate columns(0 and 2):\n”, temp)
# Indexing array
temp = arr [ [0, 1, 2, 1], [3, 2, 1, 0]]
print (“\nElements at indices (0, 2), (1, 2), (2, 1),(2, 0):\n”, temp)RUN
>>>
Array with first 2 rows and alternate columns(0 and 2):
[[1 0]
[4 7]]
Elements at indices (0, 2), (1, 2), (2, 1) , (2, 0):
[-4 7 -7 4]
>>>
  • As arrays can be multidimensional, you need to specify a slice for each dimension of the array.

Leave a Reply

Your email address will not be published. Required fields are marked *