Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

hashing elements of objects in a list to create quick lookup [python]

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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"]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading