How To Do Array Slicing By Numpy In Python
We can use the Slicing feature to move the elements from one given Index to another Given Index.
We can pass slice instead of index like this format: [start:end].
In the slicing feature we have the provision to define the step also and we can define the steps like this format [start:end:step]
If we don't pass start by default the value for start will be considered as 0
If we don't pass end by default the value for start will be considered as length of array in that dimension
If we don't pass step then by default the value of step will be considered as 1
How To Print the values in the array with The Slice Feature
Now Let's look at the some of the examples in below.How To Slice elements from index 1 to index 5 from the following array:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5])
Output:
[2,3,4,5]
Explanation:
For the above example if you will see with the arr array we have passed two integer values 1 and 5.
Here 1 is the start and 5 is the end.
So with 1 we are fetching the value 2 which resides at index 1 and from this value we are fetching upto the value index 5-1 and that is Index 4.Here in Slicing the end value will consider the index as end-1.So in this array arr it is fetching the values starting from Index 1 to Index 4 and that is [2,3,4,5]
How To Print The Array Values From A Specific Index To Last Of The Array
Here we will see how we can print the array values from Index 5 to the last of the array
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[3:])
Output:
[4,5,6,7]
Explanation:
Here in the array arr we are passing only one integer and that is the index of the array 3.Which is taking the value 4 and as we have not mentioned the End attribute so it is taking the value of upto the last element of the array arr.Hence it is printing the values [4,5,6,7]
How To Slice elements from the beginning Of The Array In Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[:5])
Output:
[1,2,3,4,5]
Explanation:
Here we have passed the index value as 5 as the End attribute but did not provide any value for the Start attribute.Hence it will consider the value which is at 0th index as the start value and it will consider the value upto End-1 value which 5-1 =4th Index of the array.
Hence the output is
[1,2.3,4,5]
How To Access Array Elements By The Negative Slicing
To use the negative slicing you need to use the - operator
Slice from the index 3 from the end to index 1 from the end:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[-3:-1])
Ouput:
[5 6]
Explanation:
Here in the negetive slicing the scanning of the array starts from the end.Hence with the integer 3 and 1
actually it's start the slicing from 3-1=2nd Index from the end and taking upto 1st Index from the end.
Hence it is showing the output as [5,6]
How you can use the step value for doing the slicing
If you want to return or print every other element for the below array from the index 1 to index 5 you can do in this below way
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[1:5:2])
Output:
[2,4]
Explanation:
Here with the first two integer values you atre actually providing the start and end of the Array.So the scanning will start from Index 1 to End-1 that is 5-1 = 4th Index Of the Array and that is 5.With the integer 2 you are actually giving the command to print the every alternate value of the array defined in the range.So it is printing
[2,4]
Comments
Post a Comment
souvikdutta.aec@gmail.com