Home > Back-end >  hashing elements of objects in a list to create quick lookup [python]
hashing elements of objects in a list to create quick lookup [python]

Time:08-17

I have similar needs to this c# question. I have a list of classes. The class contains a list of synonyms for the name of an object. I would like to set up a data structure where I can input one of the synonyms and any class object with that synonym returns. Preferably the objects in the list won't be copied but referenced in memory. The class object can be immutable as its values won't change. Here is some dummy code to explain my situation:

class MyObject:
    
def __init__(self,name):
        self.name = name # name of an object such as "cup"
        self.synonyms = get_synonyms(name) # returns a list of strings (i.e.) [ "coffee cup", "mug", "dixie cup", . . . ]

my_objects = create_1000_MyObjects() # returns a list of 1000 of the above classes
new_data_structure = ?
objects_containing_synonym = new_data_structure.find_objects("cofee cup") # would return any object in the above list that holds "coffee cup" in its list of synonyms, so would return object cup

It's worth noting there might be more than one object needing to be returned. I want to use some hash solution like a dictionary so that lookup is fast.

CodePudding user response:

A dict mapping one synonym to a list of objects will do. collections.defaultdict eliminates the need to first test the dictionary to see if a key exists.

import collections

new_data_structure = defaultdict(list)
my_object = create the object
new_data_structure.update((synonym, my_object) for synonym in my_object.synonyms)
all_coffee_cup_objects = new_data_structure["coffee_cups"]
  • Related