I am looking for a Putin way or a more efficient way to solve this problem. I have a dictionary that has many meanings (duplicates are allowed on all keys). Given a list, I have to create a dictionary that maps each category to an element using the key from the main dictionary. I will give an example to illustrate.
Main vocabulary
{ "KeyA": ['Aron', 'Ranom Value', 'Abhishek'], "KeyB": ['Ball', 'Foo', 'Bar', 'Badge', 'Dog'], "KeyZ": ['Random Value', 'Foo', 'Bar'] }
entrance
['Foo', 'Bar', 'Dog', 'Aron']
Output
{ "KeyA": ['Aron'], "KeyB": ['Bar', 'Foo', 'Dog'], "KeyZ": ['Foo', 'Bar'] }
My current thoughts
Invert individual elements in sets as keys, and then perform a search.
{ 'Aron' : ['KeyA'], 'Foo' : ['KeyB', 'KeyZ'], 'Bar' : ['KeyB', 'KeyZ'], 'Random Value' : ['KeyA', 'KeyZ'] }
I would initialize the inverted dictionary by going through each element in each set. The approximate time to create such a dictionary is O (n). Look for the item in the list in the inverted dictionary created in this way. Say the value Bar . Create a new dictionary using the 'Bar': ['KeyB', 'KeyZ'] . The resulting dictionary will be {'KeyB': ['Bar'], 'KeyZ': ['Bar']} . For the next element, I will need to do some accounting work in the existing dictionary, for example, the key exists or not, if so, add the existing list, etc.
Use the in operator on the given mapping (check that it belongs) to each key
The main dictionary and input list will in most cases be quite small. (less than 500 unique items in all sets together). Therefore, I could check the membership in the set returned by each key and create a dictionary. This is obviously less effective, but works in most cases.
I have a few more manipulations that are similar to the example above. I do not want to do manual bookkeeping for everyone, because they are error prone and slower than the built-in functions.
What I need?
- Better approaches (faster algorithm)
- Built-in functions in itertools because they are faster
- Third party library
- Some esoteric considerations that a normal Python user would not think about?