How to find the index of a string in a list of strings which contains specific substring in Python

2 Answers

0 votes
import numpy as np

lst = ['Python is', 'a high-level', 'general-purpose', 'programming language']

indices = np.char.find(lst, "purpose")
index = np.where(indices >= 0)[0][0]

print(index)

   
   
   
'''
run:
   
2
   
'''

 



answered Feb 19, 2023 by avibootz
0 votes
import numpy as np

lst = ['Python is', 'a high-level', 'general-purpose', 'programming language']

index =  np.where(np.char.find(lst, "purpose")>=0)[0][0]

print(index)

   
   
   
'''
run:
   
2
   
'''

 



answered Feb 19, 2023 by avibootz
...