Stupid420
Stupid420

Reputation: 1419

Consistent char to int

I am working on implementing the RNN model for which I need to convert all my characters into integers based on the dictionary as follows.

charset={'!',
 '#',
 '(',
 ')',
 '+',
 '-',
 '/',
 '1',
 '2',
 '3',
 '4',
 '=',
 'B',
 'C',
 'E',
 'F',
 'H',
 'I',
 'N',
 'O',
 'P',
 'S',
 '[',
 '\\',
 ']',
 'l',
 'r'}

I use the following method to convert charset into integers based on a dictionary as follows.

char_to_int = dict((c,i) for i,c in enumerate(charset))

Which gives me the following output.

{'2': 0, 'F': 1, '-': 2, 'O': 3, '1': 4, 'E': 5, '4': 6, '!': 7, 'H': 8, 'S': 9, '/': 10, '\': 11, '#': 12, 'l': 13, '=': 14, 'P': 15, 'C': 16, '+': 17, 'r': 18, 'B': 19, '(': 20, ')': 21, ']': 22, '3': 23, '[': 24, 'I': 25, 'N': 26}

Now I close my Jupyternotebook and load the data again and run the above two lines of code again. This time the mapping is different as follows.

{']': 0, '\': 1, '(': 2, '=': 3, '!': 4, 'F': 5, '3': 6, 'S': 7, '4': 8, 'N': 9, '+': 10, 'l': 11, 'H': 12, 'E': 13, 'C': 14, 'I': 15, '-': 16, 'B': 17, ')': 18, 'P': 19, '[': 20, 'r': 21, '1': 22, '/': 23, '2': 24, '#': 25, 'O': 26}

How can I get consistent char_to_int every time I run it again.

Upvotes: 0

Views: 49

Answers (1)

Amitai Irron
Amitai Irron

Reputation: 2055

set objects in Python do not have order. If you make charset a list instead of a set, then your result will be consistent between runs.

Upvotes: 2

Related Questions