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
<code>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']
</code>
<code>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']
</code>
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
<code>[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
</code>
<code>[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
</code>
[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:
<code>[('the pear tree is not ready yet','pear'), ('we have lots of pear trees','pear'), ('apples are yummy','apple')]
</code>
<code>[('the pear tree is not ready yet','pear'), ('we have lots of pear trees','pear'), ('apples are yummy','apple')]
</code>
[('the pear tree is not ready yet','pear'), ('we have lots of pear trees','pear'), ('apples are yummy','apple')]