Monday, November 18, 2024
Google search engine
HomeLanguagesnumpy.index() in Python

numpy.index() in Python

numpy.core.defchararray.index(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range But if substring is not found, it raises ValueError.

Parameters:
arr : array-like or string to be searched.
substring : substring to search for.
start, end : [int, optional] Range to search in.

Returns : An integer array with the lowest index of found sub-string, raises ValueError if substring is not found.

Code #1:




# Python Program illustrating 
# numpy.char.index() method 
import numpy as np 
  
arr = ['this is Lazyroar for geek']
  
print ("arr : ", arr)
  
print ("\nindex of 'Lazyroar' : ", np.char.index(arr, 'Lazyroar'))


Output:

arr :  ['this is Lazyroar for geek']

index of 'Lazyroar' : [8]

 
Code #2:




# Python Program illustrating 
# numpy.char.index() method 
import numpy as np 
  
arr = ['this is Lazyroar for geek']
  
print ("\nindex of 'Lazyroar' : ", np.char.index(arr, 'Lazyroar', start = 2))
print ("\nindex of 'Lazyroar' : ", np.char.index(arr, 'Lazyroar', start = 10))
print ("\nindex of 'geek' : ", np.char.index(arr, 'geek', start = 10))


Output:

index of 'Lazyroar' : [8]
ValueError: substring not found
index of 'geek' : [18]
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments