'Is there a better way to solve this using a dictionary?

I am trying to solve the following problem:

A sample csv dataset looks like this (There a total of 1000 rows in the dataset):

enter image description here

The questions I am trying to solve are:

  • Implement AND conditions, e.g. steel keyboard should match only item names which contain both steel and keyboard somewhere (not necessarily in that order)
  • Implement OR conditions, e.g. steel keyboard should match item names steel table and wooden keyboard because they each contain one of our search terms
  • Implement numeric range queries, e.g. steel keyboard that are priced between $40 and $70

I have solved the problems using the following approach, but I feel using a dictionary would have made it simpler:

class SimpleSearch: 
    
    def __init__(self,path):
        self.df = pd.read_csv(path)
    
        
    def match_keyword(self, pattern):
        self.df['matches'] = self.df['name'].str.findall(pattern).apply(lambda x: list(set(x)))
        
        
        ids = []
        for i in self.df.itertuples():
            if i.matches != []: 
                 ids.append(i.id)
                    
        return ids
    
if __name__ == '__main__': 
    path = "random_path/file.csv"
    pattern = "steel keyboard"
    search_obj = SimpleSearch(path)
    print(search_obj.match_keyword(pattern))
  • Is there a simple way by which I could differentiate the logic for And and Or operations using a dictionary? My solution only solves for AND at this point.
  • What would be the best way to solve for numeric range queries? I couldn't think of an approach and could some help.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source