Python program to find the occurrence of substring in the string
Given a list of words, extract all the indices where those words occur in the string.
Input : test_str = ‘geeksforgeeks is best for geeks and cs’, test_list = [“best”, “geeks”]
Output : [2, 4]
Explanation : best and geeks occur at 2nd and 4th index respectively.
Input : test_str = ‘geeksforgeeks is best for geeks and cs’, test_list = [“best”, “geeks”, “is”]
Output : [1, 2, 4]
Explanation : is, best and geeks occur at 1st, 2nd and 4th index respectively.
Method #1 : Using list comprehension + split() + index()
In this, we perform the task of getting words from sentences using split(), and then match words from the string list to extracted strings using index().
Python3
test_str = 'geeksforgeeks is best for geeks and cs'
print ( "The original string is : " + str (test_str))
test_list = [ "best" , "geeks" , "cs" ]
res = [test_str.split().index(ele)
for ele in test_str.split() if ele in test_list]
print ( "The indices list : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks and cs
The indices list : [2, 4, 6]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using list comprehension + enumerate() + split()
This is yet another way in which this task can be performed. In this task of getting indices is done using enumerate().
Python3
test_str = 'geeksforgeeks is best for geeks and cs'
print ( "The original string is : " + str (test_str))
test_list = [ "best" , "geeks" , "cs" ]
res = [idx for idx, ele in enumerate (test_str.split()) if ele in test_list]
print ( "The indices list : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks and cs
The indices list : [2, 4, 6]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: The idea is to use a simple for loop to iterate through each word in the string and check if it is present in the given list of words
Steps:
- Initialize a string test_str with some sample text.
- Create a list test_list of words whose positions we want to find in the string.
- Create an empty list res to store the positions of the words in the string.
- Use the split() method to split the string into a list of words, so that we can iterate over each word.
- Use a for loop to iterate through each word in the string, along with its index.
- Check if the current word is present in the test_list using the in operator.
- If the word is present in the list, append its index to the res list.
- Print the final list of indices.
Python3
test_str = 'geeksforgeeks is best for geeks and cs'
test_list = [ "best" , "geeks" , "cs" ]
res = []
for idx, word in enumerate (test_str.split()):
if word in test_list:
res.append(idx)
print ( "The indices list : " + str (res))
|
Output
The indices list : [2, 4, 6]
Time Complexity: O(n), where n is the number of words in the string.
Auxiliary Space: O(k), where k is the number of occurrences of the words in the given list in the string.
Last Updated :
21 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...