Home > Mobile >  Trying to create a function to search for a word in a string
Trying to create a function to search for a word in a string

Time:10-10

it's my first time posting but I have a question regarding trying to create a function in python that will search a list of strings and return any words that I am looking for. Here is what I have so far:

def search_words(data, search_words):
    keep = []
    for data in data:
        if data in search_words:
           keep.append(data)
    return keep

Here is the data I am searching through and the words I am trying to find:

data = ['SHOP earnings for Q1 are up 5%',
        'Subscriptions at SHOP have risen to all-time highs, boosting sales',
        "Got a new Mazda, VROOM VROOM Y'ALL",
        'I hate getting up at 8am FOR A STUPID ZOOM MEETING',
         'TSLA execs hint at a decline in earnings following a capital expansion program']
words = ['earnings', 'sales']

Upon doing print(search_words(data=data, search_words=words)) my list (keep) returns empty brackets [ ] and I am unsure of how to fix the issue. I know that searching for a word in a string is different than looking for a number in a list but I cannot figure out to modify my code to account for that. Any help would be appreciated.

CodePudding user response:

You can use the following. This will keep all the sentences in data that contain at least one of the words:

keep = [s for s in data if any(w in s for w in words)]

CodePudding user response:

Since they are all strings, instead of looping over them all just combine them all and search that. Also make words a set:

[word for word in ' '.join(data).split() if word in words]

Using regex:

re.findall('|'.join(words), ''.join(data))

['earnings', 'sales', 'earnings']
  • Related