Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
For each element return the lowest index in the string where substring is found in Python
To return the lowest index in the string where substring sub is found, use the numpy.char.find() method in Python Numpy. The method returns the output array of ints. Returns -1 if sub is not found. The first parameter is the input array. The second parameter is the substring to be searched
Steps
At first, import the required library −
import numpy as np
Create a One-Dimensional array of strings −
arr = np.array(['KATIE', 'JOHN', 'KATE', 'AmY', 'BRADley'])
Displaying our array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nNumber of elements in the Array...\n",arr.size)
To return the lowest index in the string where substring sub is found, use the numpy.char.find() method −
print("\nResult (find)...\n",np.char.find(arr, 'AT'))
Example
import numpy as np
# Create a One-Dimensional array of strings
arr = np.array(['KATIE', 'JOHN', 'KATE', 'AmY', 'BRADley'])
# Displaying our array
print("Array...\n",arr)
# Get the datatype
print("\nArray datatype...\n",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)
# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",arr.size)
# To return the lowest index in the string where substring sub is found, use the numpy.char.find() method in Python Numpy
# The method returns the output array of ints. Returns -1 if sub is not found.
# The first parameter is the input array
# The second parameter is the substring to be searched
print("\nResult (find)...\n",np.char.find(arr, 'AT'))
Output
Array... ['KATIE' 'JOHN' 'KATE' 'AmY' 'BRADley'] Array datatype... <U7 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result (find)... [ 1 -1 1 -1 -1]
Advertisements