Professional Documents
Culture Documents
19-Ntu-Kc-Te-033 L-10
19-Ntu-Kc-Te-033 L-10
19-Ntu-Kc-Te-033 L-10
Lab-10
1.NumPy Searching Arrays
Searching Arrays
You can look for a specific value in an array and return the indexes that match.
Example-1:
Input
Output
Example-2:
Input
1|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
Output
Example-3
Input
Output
Search Sorted
There is a method called searchsorted() which performs a binary search in the
array, and returns the index where the specified value would be inserted to
maintain the search order.
Example-4:
2|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
Input
Output
2.NumPy Sorting Arrays
Sorting Arrays
Sorting means putting elements in an ordered sequence.
The NumPy ndarray object has a function called sort(), that will sort a specified
array.
Example-1
3|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
Output
Example-2
Input
Output
Example-3
Input
4|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
Output
Example-4
Input
Output
3.NumPy Filter Array
Filtering Arrays
Getting some elements out of an existing array and creating a new array out of
them is called filtering.
5|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
If the value at an index is True that element is contained in the filtered array, if the
value at that index is False that element is excluded from the filtered array.
Example-1:
Input
Output
Example-2
Create a filter array that will return only values higher than 42:
6|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
Input
Output
We can directly substitute the array instead of the iterable variable in our condition
and it will work just as we expect it to.
Example-3
Create a filter array that will return only values higher than 42:
7|Page
Talha Saif Lab-10 19-NTU-KC-TE-033
Input
Output
8|Page