Have an array as shown below.
arrayy = ['top,tree,branch,bla-top,tree,ascb-red/blue', 'tree,leaves,mmn-tree,leaves,mscb-gra/gre', 'leaves,bird,responder,mon-leaves,bird,ascb-yoo/yee','tree,leaves,mount-road,cycle-roo/soo']
- Is there a simple way to find the index which contains a sub-string inside a list of string ?
- For eg, I wanted to search for "leaves,bird*-leaves,bird*" and wanted to return the index for the same.
Tried the below code,
def find_index_sub_string(needle,haystack):
return [i for i, x in enumerate(haystack) if needle in x]
- Able to search for leave,bird but unable to search like "leaves,bird*-leaves,bird*"
Is there any better way to search and get the require string ?
UPDATE:
Got it working with below code.
search_re = re.compile("leaves,bird.*-leaves,bird.*")
for i in range (len(arrayy)):
if re.match(search_re, arrayy[i]):
print i
leaves,bird*-leaves,bird*search you would need to usesearch_re = re.compile("leaves,bird.*-leaves,bird.*")out of your loop and then do ifsearch_re.search()within your loop"leaves,bird*-leaves,bird*"is not present in any of the string present inarrayyright? I didn't properly understand the question you asked. Can someone explain it to me please.