Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

search for elements of a list as substring in another list python

I have 2 lists. I want to find the elements in ls2 where any element of ls1 is a substring. I would like to return a list of ls2 elements along with the substring that was searched and found from ls1

ls1 = ['apple','banana','pear']
ls2 = ['strawberry is not here',
       'blueberry is over there',
       'the pear tree is not ready yet',
       'we have lots of pear trees',
       'apples are yummy']

Both of these return a partial answer

[ls1[j] for j in range(len(ls1)) if any(ls1[j] in x for x in ls2)] # returns elements from ls1 alone
[i for i in ls2 if any(w in i for w in ls1)] # returns elements from ls2 alone

What I would like is to see:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[('the pear tree is not ready yet','pear'),
 ('we have lots of pear trees','pear'),
 ('apples are yummy','apple')]

>Solution :

Get rid of the any aggregation and just do nested loops:

result = [(sentence, word) 
          for sentence in ls2 for word in ls1 
          if word in sentence]

Alternatively, use itertools.product to get the same effect as the nested loops:

import itertools
result = [(sentence, word) 
          for sentence, word in itertools.product(ls2, ls1) 
          if word in sentence]

Try it online

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading