1

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.

6
  • Many to many, or one to many (e.g., one synonym to many MyObject instances)? Commented Aug 16, 2022 at 18:13
  • one_found_object is an awkward name for all of the objects holding that synonym. Did you want to randomly select one of them? Commented Aug 16, 2022 at 18:21
  • I think this concept refers to many to many (correct me if I'm wrong) "coffee cup" could be listed under more than one object. And each object has more than one synonym. However for each search I'd be searching one synonym and getting a list of objects that contains that synonym. Commented Aug 16, 2022 at 18:21
  • @tdelaney true, poorly named, I'll edit. Commented Aug 16, 2022 at 18:22
  • Right, but it looks like you want a one-to-many index. You could also want to find all objects that have at least one synonym in common, but that would be a different solution than what I provided. Commented Aug 16, 2022 at 18:23

1 Answer 1

2

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"]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Just to check. the defaultdict is storing references to the objects and not creating new ones?
@AlexanderOrman - Right. That is generally the case with assignment via = in python. The object's reference count goes up and its memory pointer is added to the thing on the left hand side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.