0

I have the following translation dictionary:

{0: {'a', 'b', 'c'},
1: {'a', 'b', 'c', 'd'},
2: {'k', 'b', 'e', 'a', 'n'}}

And I want to 'reverse' it to be attributes to keys (keys here are a form of id). Given a set of attributes, give me the relevant id (key).

For example, given {'a', 'b', 'c'} return 0.

What is the best practice to do this? The attributes can come in different order that's why I am using sets. Should I insert it into a dataframe (translation table)? Or there is another solution?

SteveS
  • 3,789
  • 5
  • 30
  • 64

2 Answers2

1

you can use a Series to achieve this in pandas:

import pandas as pd

x = {0: {'a', 'b', 'c'},
1: {'a', 'b', 'c', 'd'},
2: {'k', 'b', 'e', 'a', 'n'}}

lookup = pd.Series(x)
print(lookup[lookup.values == {'a', 'b', 'c'}])
# 0    {c, b, a}
# dtype: object
match
  • 10,388
  • 3
  • 23
  • 41
  • Is it scalable? And thanks for the approach! Can't understand why people downvoted your answer and my question... – SteveS Jan 06 '22 at 13:11
  • 2
    I did not downvote the answer or the question, but I am pretty sure that this requires scanning through the entire Series each query. – hilberts_drinking_problem Jan 06 '22 at 13:14
  • Yes - this isn't a 'cached' approach, it's just a way to achieve this in `pandas` if that's a broader requirement of the task. – match Jan 06 '22 at 13:22
1

Not sure why you want to use pandas. Here is a pure python solution.

You can reverse the dictionary to use a frozenset as key:

d = {0: {'a', 'b', 'c'},
     1: {'a', 'b', 'c', 'd'},
     2: {'k', 'b', 'e', 'a', 'n'}}

rev_d = {frozenset(k): v for v,k in d.items()}

rev_d[frozenset({'a', 'c', 'b'})]
# 0
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Great, I will check with Prof. Google what are the differences between set and frozen set. – SteveS Jan 06 '22 at 13:21
  • 1
    @SteveS I provided a link to the doc ;) this is like a set but immutable – mozway Jan 06 '22 at 13:23
  • 1
    it is hashable because it is immutable ;) – mozway Jan 06 '22 at 13:31
  • Just to clarify, I have seen in the last answer in the following question Serge's comment saying that it's the hashable part that matters. Can you please explain why it's the immutable part? @mozway https://stackoverflow.com/questions/36555214/set-vs-frozenset-performance – SteveS Jan 06 '22 at 13:50
  • 1
    To make it clear, the **hashability** is was is needed to be used as key, but the fact that frozensets are hashable is because they are **immutable**, it wouldn't make much sense to store an object in a hash table (a dictionary) if the object can then change (which would change its hash). I hope this clarifies ;) – mozway Jan 06 '22 at 13:55
  • Yes, thanks a lot for your answer and explanation :) – SteveS Jan 06 '22 at 15:55