-4

Example

description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']

my_list = ['travel','fire']

I would like to check if ANY of the words on my_list is in description, if so don't do anything. If my_list is not in description I would like to return the string 'Keywords not found'.

How would I go about writing this code?

  • Does "the usual way" help you? – Jongware Dec 26 '17 at 00:07
  • Will your words in `my_list` contain phrases? – Tai Dec 26 '17 at 00:37
  • 3
    Possible duplicate of [Check list of words in another string](https://stackoverflow.com/questions/3271478/check-list-of-words-in-another-string) – jscs Dec 26 '17 at 00:41
  • Possible duplicate of [Check if list item contains items from another list](https://stackoverflow.com/q/11328940) – jscs Dec 26 '17 at 00:42
  • Possible duplicate of [Check if a python list item contains a string inside another string](https://stackoverflow.com/q/4843158) – jscs Dec 26 '17 at 00:42

4 Answers4

4

You can use all with a double list comprehension:

description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
my_list = ['travel','fire']
def response():
   return "found" if any(i in b for i in my_list for b in description) else "Keywords not found"
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

Save words in a set and check whether a word in my_list is in the set. This only works when there is no phrases in my_list. i.e. all words in my_list is a unigram.

description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
my_list = ['travel','fire']
set_words_in_description = set()
for s in description:  
    # add new words in set_words_in_description
    set_words_in_description.update(set(w for w in s.split())) 

Using isdisjoint

def find_word_method_disjoint(my_list, set_words_in_description):
    # check if my_list is disjoint with set_wrods_in_description
    return not set_words_in_description.isdisjoint(my_list)

%timeit find_word_method_disjoint(my_list, set_words_in_description)
189 ns ± 1.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

%timeit response()  # function given by the accepted answer.
572 ns ± 9.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Tai
  • 7,684
  • 3
  • 29
  • 49
0

You could use re.findall() to extract all the words from the sentences in description, and check if any of the words from my_list exist in it:

import re

def find_words(words, text):
    desc_words = re.findall(r'[^\s,.]+', "".join(text))

    for word in words:
        if word in desc_words:
            return "found"

    return "Keywords not found"

Which outputs:

>>> description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
>>> my_list = ['travel','fire']
>>> find_words(my_list, description)
found

Or you could use this set() approach:

def find_words(words, text):
    return "found" if set(words).intersection(re.findall(r'[^\s,.]+', "".join(text))) else "Keywords not found"

Note: You will have to update your regular expression if you come across sentences with different punctuation other than ,..

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
0

You can try set something like this ?

description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']

my_list = ['travel','fire']

flat=[k for i in description for k in i.split()]


print(any({i}.issubset(set(flat))for i in my_list))

output:

True
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88