T
T
tvoyasosiska2018-08-09 16:39:57
Python
tvoyasosiska, 2018-08-09 16:39:57

How to find out if one of the words in the dictionary is contained in the text?

There is a dictionary . It is necessary to check whether env.result contains any of the words from kolbacka (env.result is my function, it returns a list of products)
kolbacka = ['ananasik', 'sosiska']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Serge Litvinov, 2018-08-09
@litvinovserge

if you already have a function that converts text into a list, then it’s enough to do something like
you need to iterate over the kolbaska list

for each in kolbacka:
    if each in env.result:
        print('В тексте есть колбаски')
    else:
        print('Ничего не найдено.')

A
Anatoly, 2018-08-09
@trofimovdev

As an option (with your permission, I will replace c with s ):

kolbaska = ['ananasik', 'sosiska']
text = 'в магазине есть ananasik'
print('В тексте что-то есть' if len(set(kolbaska) &
                                    set(text.split(' '))) != 0
                             else 'Ничего не найдено')

kolbaska = ['ananasik', 'sosiska']
text = 'в магазине есть ananasik'
print('В тексте что-то есть' if any(word in text for word in kolbaska)
                             else 'Ничего не найдено')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question