Python - Partial Search in List




NO LOOPING KEYWORD SEARCH

Here in this post, I have found a way to retrieve items from list with part of keyword in quicker way, but I m not sure that would be feasible for large datasets. But this works faster for moderate data.

Say if you are having a list of strings and you have keyword to search in the list and get the index or the element from the list, but the goal is to fetch with partial match.


import re

list_ = ['hello','world hello','I m saying hello to you','what do you want','where would you like to go','Hello is the word used to greet you','HELLO BUDDY','You know what, this day is the best day I have ever had','dataservicer is the best website that provides needed data to the end users','Hello everyone','ajaj','What you want','hllo','helLO']
list_

['hello', 'world hello', 'I m saying hello to you', 'what do you want', 'where would you like to go', 'Hello is the word used to greet you', 'HELLO BUDDY', 'You know what, this day is the best day I have ever had', 'dataservicer is the best website that provides needed data to the end users', 'Hello everyone', 'ajaj', 'What you want', 'hllo', 'helLO']





Whatever be the length of the list, you have to convert to string. String search is the fast way of search rather than looping


list_to_str = '\t'.join([ele for ele in list_])
list_to_str

'hello\tworld hello\tI m saying hello to you\twhat do you want\twhere would you like to go\tHello is the word used to greet you\tHELLO BUDDY\tYou know what, this day is the best day I have ever had\tdataservicer is the best website that provides needed data to the end users\tHello everyone\tajaj\tWhat you want\thllo\thelLO'




Right after converting into string, regular expression is applied on the pattern, with finditer function, it is possible to find the index of the occurrences and then we can capture over there

search_term = 'hello'
idx_ = 0
obtained_matches = []
prev_end = 0
for m in re.finditer('\\t*?'+search_term+'.*?\\t|$',list_to_str,re.I):
    start_index = prev_end
    if idx_ == 0:
        start_index = idx_
    
    extracted_pattern = list_to_str[start_index:m.end()].split('\t')
    if extracted_pattern[-1] == '':
        obtained_matches.append(extracted_pattern[-2])
    else:
        if search_term in extracted_pattern[-1].lower():
            obtained_matches.append(extracted_pattern[-1])
    prev_start = m.start()
    prev_end = m.end()
  

obtained_matches

['hello', 'world hello', 'I m saying hello to you', 'Hello is the word used to greet you', 'HELLO BUDDY', 'Hello everyone', 'helLO']



And finally the required result is achieved





I m not saying that this is the best way. I have found a way for minimal datasets.


how to find string in list python how to check string in list python,how to find string position in list python,how to find part of string in list python,how to check string contains in list python,how to find duplicate strings in a list python list search in python,list search engine,list search,list search in data structure,list search in java How to find elements from list with partial search, partial search data retrieval,how to find a string from a list,quick way to get string occurences from list,partial search in mongodb,partial search,partial search tree,partial search in sql,partial search in elasticsearch,partial search in excel

No comments