Dictionary linking elements from multiple sets together (in python)?

I'm looking for a dictionary-like data-structure (preferably in numpy or pandas) to deal with elements from different sets being grouped together

For example, I have data about People giving Ratings to different Cats.

If D is the data structure that contains everything, I'd like it to have something like the following functionality:

D.add_entry(person="Alice", cat="cheshire", rating="seven")

alice_cats, alice_ratings = D.index_by_person["Alice"]
for cat, rating in zip(alice_cats, alice_ratings):
  print(f"Alice rated {cat} as {rating}")

cheshire_people, cheshire_ratings = D.index_by_cat["cheshire"]
for person, rating in zip(cheshire_people, cheshire_ratings):
  print(f"{person} rated cheshire as {rating}")

seven_people, seven_cats = D.index_by_rating["eight"]
for person, cat in zip(seven_people, seven_cats):
  print(f"{person} rated {cat} as seven")

people, cats, ratings = D.all_entries
for person, cat, rating in zip(people, cats, ratings):
  print(f"{person} rated {cat} as {rating}")

This data structure has to be efficient for 100k+ entries. I was thinking of making k dictionaries (one for each set), but I'd like a cleaner solution from an established library, if such a thing exists.

Read more here: https://stackoverflow.com/questions/66997742/dictionary-linking-elements-from-multiple-sets-together-in-python

Content Attribution

This content was originally published by chausies at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: