How do I check if a string contains a word from a list?

For example, there is a list of

words = ["Авто", "Велосипед", "Самолет"]

And for example the string

str = "Быстрый автомобиль"

You need to return True, because the line has "auto"

Author: MaxU, 2018-11-09

4 answers

Using regular expressions, you can do such a check without a loop:

In [12]: import re

In [13]: chk_pat = '(?:{})'.format('|'.join(words))

In [14]: chk_pat
Out[14]: '(?:Авто|Велосипед|Самолет)'

In [15]: s = "Быстрый автомобиль"

In [16]: bool(re.search(chk_pat, s, flags=re.I))
Out[16]: True

In [17]: bool(re.search(chk_pat, 'строка', flags=re.I))
Out[17]: False

PS if the list of check words is too large 10+K bytes, then it is probably better not to use such long regular expressions

 3
Author: MaxU, 2018-11-09 12:34:33
def is_part_in_list(str_, words):
    for word in words:
        if word.lower() in str_.lower():
            return True
    return False

Test:

words = ["Авто", "Велосипед", "Самолет"]
str_ = "Быстрый автомобиль"
print(is_part_in_list(str_, words))

Conclusion:

True
 3
Author: MarianD, 2018-11-09 12:37:23
words = ["Авто", "Велосипед", "Самолет"]

str = "Быстрый автомобиль"

for s in words:
    if str.lower().find(s.lower()) != -1:
        print("True")
        break
 2
Author: Юрий, 2018-11-09 12:19:50
print(any(word.lower() in str.lower() for word in words))
 1
Author: sashaaero, 2018-11-09 19:51:30