Updated April 3, 2023
Introduction to NumPy find
In python, numpy.core.defchararray.find() is a function that returns the smallest index in the string for every element where the substring is identified. In python, there are several libraries available to perform different functions, and NumPy is one such python library that is helpful for array functionalities. In addition to that, many linear algebras, matrix operations, and Fourier transform operations can also be used using this library. Let us see more on the find() function in the below sections.
Syntax
Following is the syntax of the find function in numpy.
numpy.core.defchararray.find(arr, sub, start=0, end=None)
The parameters of this function are:
- arr: string or array-like that has to be searched.
- sub: substring that has to be searched for.
- start, end : [ int, optional ] Range that has to search in.
The return value of this function:
The smallest index in the string for every element where the substring is identified.
If no substring is found, -1 will be returned.
How to Find work in NumPy?
Let us see the working of NumPy Find using an example.
Suppose there is a string ‘thousand years’ and stored it in variable str as shown below.
str= 'thousand years'
If the lowest index of the substring in this string has to be found, use the function find() in numpy as mentioned below.
a = np.char.find( str , 'world', start=0, end=None)
The index is stored in the variable a, and later it can be printed.
Note:
In the new development, the ‘chararray’ class is not recommended as it exists for backward compatibility in Numarray. From the numpy version 1.4, if arrays of strings are needed, it is suggested to use arrays of ‘string_’, ‘dtype’, ‘unicode_’, ‘object_’, and use the available free functions in the module ‘numpy.char’ for fast vectorized string tasks. But, few methods will only be accessible if the matching string method is present in the version of Python you are using. Moreover, ‘numpy.char’ is the ideal alias for ‘defchararray’.
Examples of NumPy find
As already mentioned above, let us see some sample programs on NumPy find.
Example #1
Python Program that demonstrates the find function by finding the substring ‘ap’ in the array elements.
Code:
# Python Program that demonstrates the find function
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with four elements
arr1 = ['Happy' , 'swap', 'Map', ' Laptop']
print ("array is : ", arr1)
print (" \n find of substring 'ap' ", np.char.find(arr1, 'ap'))
print (" find of substring 'ap' ", np.char.find(arr1, 'ap', start = 0))
print (" find of substring 'ap' ", np.char.find(arr1, 'ap', start = 8))
Output:
In this program, first, import the numpy library and set the alias name as np. After that, declare an array with four elements such as [‘Happy’ , ‘swap’, ‘Map’, ‘ Laptop’]. Then, print the original array. Once this is done, print the lowest index of substrings by using different start values. As no substring is found, -1 is also returned.
Example #2
Python Program that demonstrates the find function by finding the substring ‘world.’
Code:
# Python Program that demonstrates the find function
# import the numpy library and set alias name as np
import numpy as np
# Declare a string
str= 'Helloworld'
#find the index of the substring 'world' and store it in a
a = np.char.find( str , 'world', start=0, end=None)
#print the index
print (a)
Output:
First, the numpy library is imported in this program, and an alias name is set as np. Then, a string ‘Helloworld’ is declared. Once this is completed, the substring ‘world’ index is found and stored in variable a. After that, the index is printed on executing the code. Here, 5 is the index of the substring ‘world’.
Example #3
Python Program that demonstrates the find function by finding the substring ‘World.’
Code:
# Python Program that demonstrates the find function
# import the numpy library and set alias name as np
import numpy as np
# Declare a string
str= 'Helloworld'
#find the index of the substring 'World' and store it in a
a = np.char.find( str , 'World', start=0, end=None)
#print the index
print (a)
Output:
In this program also, first, the numpy library is imported, and an alias name is set as np. Then, a string ‘Helloworld’ is declared. Once this is completed, the substring ‘World’ index is found and stored in variable a. The difference in this program from the above program is the case sensitivity. Here, ‘W’ in World is a capital letter, and in the above program, ‘w’ is a small letter. Since the original string has ‘w’ as a small letter and python is case sensitive, the find() function could not identify World in the given string. After that, the index is printed on executing the code. Here, -1 is returned as the index of the substring ‘World’ is not found.
Example #4
Python Program that demonstrates the find function by finding the substring ‘al’ in the array elements.
Code:
# Python Program that demonstrates the find function
# import the numpy library and set alias name as np
import numpy as np
# Declare an array with four elements
arr1 = ['Walk' , 'Talk', 'Call', ' Small']
print ("array is : ", arr1)
print (" \n find of substring 'al' ", np.char.find(arr1, 'al'))
print (" find of substring 'al' ", np.char.find(arr1, 'al', start = 0))
print (" find of substring 'al' ", np.char.find(arr1, 'al', start = 2))
Output:
In this program, an array is declared with four elements: [‘Walk’, ‘Talk’, ‘Call’, ‘ Small’]. On executing the code, the lowest index of substrings will be returned by using different start values. As no substring is found, -1 is also returned.
Conclusion
numpy.core.defchararray.find() is a function in python that returns the smallest index in the string for every element where the substring is identified. This article will discuss different details on broadcasts, such as working and examples, in detail.
Recommended Articles
This is a guide to NumPy find. Here we discuss How Find works in NumPy and Examples, along with codes and outputs in detail. You may also have a look at the following articles to learn more –