I have a list of lists in which I want to return the contents of strings within each list that match specific keywords.
THIS IS ORIGINAL LIST:
list_orig = [['summary of the', 'cold weather', 'bother me over high'], ['what is in a name?', 'reveals a lot', 'juniper relationship']]
WANT TO APPLY THIS KEYWORD SEARCH:
keywords = ['summary', 'indicates','suggesting', 'relationship', 'reveals']
RESULT DESIRED:
list_refine = [['summary of the'], ['reveals a lot', 'juniper relationship']]
So far, i have the code to apply to a single list but I don't know how to look that over all the lists. HERE IS CODE FOR ONE LIST:
string1 = list_orig
substr1 = keywords
def Filter(string, substr):
return [str for str in string if
any(sub in str for sub in substr)]
print(Filter(string1, substr1))
HERE IS RESULT FOR 1 LIST:
['summary of the']
I researched so many ways to loop over the lists of lists. Here is 1 attempt.
for item in string3:
new.append([])
for item in items:
item = Filter(string1, substr1)
new[-1].append(item)
item
just got a blank list Thanks everyone! Appreciate it :)