A
A
AlmazKayum2022-04-12 20:52:59
Python
AlmazKayum, 2022-04-12 20:52:59

How to make searching faster in Python?

There is a tuple of tuples of row id and sentences, obtained in postgresql by a select.

Example,

data = ((1, 'Здравствуйте, я ваша тетя'),  (2, 'Алял ля ля, оле леле'), (3, 'Для этой ситуации необходим VPN'))


The task is
to find certain words in a sentence and give their id.

The only way I know of is to iterate over every word in sentences with a for loop
searching_word = 'необходим'
searched_ids = []
for id, sentence in data:
    if word in sentence:
        searched_ids.append(id)
        continue


When there are many rows in the database, I assume that the search will be very long.
Are there any ways to make searching fast?
Maybe through SQL, or do I need to use some kind of Python library?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2022-04-12
@bacon

The search must initially be done using SQL, or in general, use a separate indexer of the elasticsearch type

D
denislysenko, 2022-04-14
@denislysenko

You can use the list generator, it works faster than a regular loop

def func(data, searching_word):
    return [id for id, sentence in data if searching_word in sentence ]

result = func(((1, 'Здравствуйте, я ваша тетя'),  (2, 'Алял ля ля, оле леле'), (3, 'Для этой ситуации необходим VPN')), 'необходим')

print(result) # --> [3]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question