I have a set of unique strings, I want to create a unique integer identifier for each string.
Usage I want a function to move back and forth, if I give it an integer it returns the corresponding string and vice versa.
Here is how I am doing it
def str_to_int(S): integers = list(range(len(S))) my_dict = dict(zip(S,integers)) rev_dict = dict(zip(integers,S)) return my_dict, rev_dict
If I need to get the integer identifier of an item of
S, I need to call the function and then the appropriate returned dictionary.
I want something simpler, given an integer or a string, it knows, somehow automatically if it's an int or str and return the other identifier (i.e. if I give an int it returns the str identifier and vice versa). Is it possible to do it in a single function ? (if possible without being obliged to recreate dictionaries for each call)
Edit: I thought of doing to functions
str_to_int(S:set, string:str)->int and
int_to_str(S:set ,integer:int)->str but the problem is 1) that's two functions, 2) each time two dictionaries are created.